Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

快速取得option的值 #292

Closed
RubyLouvre opened this issue Feb 27, 2014 · 2 comments
Closed

快速取得option的值 #292

RubyLouvre opened this issue Feb 27, 2014 · 2 comments

Comments

@RubyLouvre
Copy link
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
Copy link
Owner Author

function hasValueAttr(strOption) {
            return /^<option(?:\s+\w+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?)*\s+value[\s=]/i.test(strOption);
        }

@RubyLouvre
Copy link
Owner Author

#292

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant