最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery value of radio button: "Uncaught syntax error, unrecognized expression" - Stack Overflow

programmeradmin3浏览0评论

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

See / 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());
    });     
});

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/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());
    });     
});
Share Improve this question asked May 6, 2011 at 13:58 simonsimon 6,11713 gold badges32 silver badges28 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 10

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.

Remove the @. $("input[name=login]")

You probably also want to use this in the callback:

$(document).ready(function() {  
    $("input[name=login]").change(function(){
      alert($(this).val());
    });     
});

Just $("input[name=login]") works for me. Never seen the @ used in that context before, is it supposed to be doing something?

Remove @ symbol from your selectors, since it doesn't exists on jQuery 1.5.2.

Here is your Fiddle updated.

You need quotes around the attribute value, 'login'. Also, I don't think you need the @ symbol:

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

Lose the @ symbol in the selector.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论