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

javascript - jQuery .change() not firing - Stack Overflow

programmeradmin2浏览0评论

Just working on some small pages to be elements of a much larger project and am pletely confused at my current problem here is my code:

<script src=".5.1/jquery.min.js"></script>
<STYLE TYPE="text/css">
  .questionBlock { font-size: x-large; color: red }
</STYLE>

<script type="text/javascript">
$(document).ready(function() { 
alert("sdsd")
$("input[@name='questionType']").change(function(){
    alert("dfdfdf");
    var selected = $("form input:radio:checked").val();
    alert(selected);
})
});

</script>

<form action="">
<input type="radio" name="questionType" value="closed" /> Closed Endeded<br />
<input type="radio" name="questionType" value="matrix" /> Matrix Question<br />
<input type="radio" name="questionType" value="open" /> Open Ended
</form>

<div class="questionBlock" id="closed">lol</div>
<div class="questionBlock" id="open">rol</div>
<div class="questionBlock" id="matrix">bol</div>

But the change event never fires, regardless of browser, I've tried using bind as well but it's driving me up the wall!

Just working on some small pages to be elements of a much larger project and am pletely confused at my current problem here is my code:

<script src="https://ajax.googleapis./ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<STYLE TYPE="text/css">
  .questionBlock { font-size: x-large; color: red }
</STYLE>

<script type="text/javascript">
$(document).ready(function() { 
alert("sdsd")
$("input[@name='questionType']").change(function(){
    alert("dfdfdf");
    var selected = $("form input:radio:checked").val();
    alert(selected);
})
});

</script>

<form action="">
<input type="radio" name="questionType" value="closed" /> Closed Endeded<br />
<input type="radio" name="questionType" value="matrix" /> Matrix Question<br />
<input type="radio" name="questionType" value="open" /> Open Ended
</form>

<div class="questionBlock" id="closed">lol</div>
<div class="questionBlock" id="open">rol</div>
<div class="questionBlock" id="matrix">bol</div>

But the change event never fires, regardless of browser, I've tried using bind as well but it's driving me up the wall!

Share Improve this question asked Mar 4, 2011 at 12:01 user644666user644666 331 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

jQuery attribute selectors don't need @ prefix (like XPath). Change your code like this:

$("input[name='questionType']").change(function(){

Here is a working version.

you need to remove the @ fiddle

$(document).ready(function() { 

    $('input[name="questionType"]').change(function(){
        alert("dfdfdf");
        var selected = $("form input:radio:checked").val();
        alert(selected);
    })
    });

jQuery does not use "@" in Xpath-style attr selectors anymore. This being the case, your selector does not match anything.

发布评论

评论列表(0)

  1. 暂无评论