单选按钮的jQuery值:“未捕获的语法错误,无法识别的表达式”(jQuery value of radio button: “Uncaught syntax error, unrecognized expression”)

我试图用$("input[@name=login]")得到一个单选按钮的值,但得到“Uncaught Syntax error,unrecognized expression”。

请参阅http://jsfiddle.net/fwnUm/以下是完整的代码:

<!-- Radio buttons to choose signin/register --> <fieldset data-role="controlgroup" data-theme="z" data-type="horizontal" > <input type="radio" name="login" id="radio-signin" value="signin" checked="checked" /> <label for="radio-signin">Signin</label> <input type="radio" name="login" id="radio-register" value="register" /> <label for="radio-register">Register</label> </fieldset> $(document).ready(function() { $("input[@name=login]").change(function(){ alert($("input[@name=login]:checked").val()); }); });

I'm trying to get the value of a radio button with $("input[@name=login]"), but am getting "Uncaught Syntax error, unrecognized expression".

See http://jsfiddle.net/fwnUm/ and here's the code in full:

<!-- Radio buttons to choose signin/register --> <fieldset data-role="controlgroup" data-theme="z" data-type="horizontal" > <input type="radio" name="login" id="radio-signin" value="signin" checked="checked" /> <label for="radio-signin">Signin</label> <input type="radio" name="login" id="radio-register" value="register" /> <label for="radio-register">Register</label> </fieldset> $(document).ready(function() { $("input[@name=login]").change(function(){ alert($("input[@name=login]:checked").val()); }); });

最满意答案

在jQuery 1.3中删除类似XPath的属性选择器。 (我们现在使用jQuery 1.6。)

只需删除@ :

$("input[name='login']").change(function(){ alert($("input[name='login']:checked").val()); });

请注意,引号也是必需的。

请参阅属性equals选择器的API参考。

XPath-like attribute selectors were removed in jQuery 1.3. (We're now on jQuery 1.6.)

Just remove the @:

$("input[name='login']").change(function(){ alert($("input[name='login']:checked").val()); });

Note that quote marks are also required.

See the API reference for the attribute equals selector.

更多推荐

login,@name,电脑培训,计算机培训,IT培训"/> <meta name="description"