Skip to content

Loading…

快速取得option的值 #292

Closed
RubyLouvre opened this Issue · 2 comments

1 participant

@RubyLouvre
Owner
            function getVal(strOption) {
                var re_attr = /\s+(\w+)(?:\s*=\s*("[^"]*"|'[^']*'|[^\s>]+))?/g;
                var re = RegExp('<option((?:' + re_attr.source + ')*)\\s*>([\\s\\S]*?)<\/option>', 'i');
                var a = re.exec(strOption);
                if (a) {
                    var t;
                    while (t = re_attr.exec(a[1])) {
                        if (t[1].toLowerCase() == 'value') {
                            var val = t[2];
                            if (val.length > 1) {
                                var c1 = val.charAt(0);
                                var c2 = val.charAt(val.length - 1);
                                if (c1 == '"' && c2 == '"' || c1 == "'" && c2 == "'")
                                    val = val.slice(1, -1);
                            }
                            return val;
                        }
                    }
                    return a[4];
                }
            }
            var a = ["<option value='xxx' id=ppp>rrrr</option>",
                "<option  id=ppp>rrrr</option>",
                "<option xxx=\"value='dd'\" id=ppp>rrrr</option>",
                "<option value=77d id=ppp>rrrr</option>"];
            for (var i = 0; i < a.length; ++i)
                a[i] = getVal(a[i]);
            alert(a.join('\n'));
@RubyLouvre
Owner
function hasValueAttr(strOption) {
            return /^<option(?:\s+\w+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?)*\s+value[\s=]/i.test(strOption);
        }
@RubyLouvre RubyLouvre closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.