javascript - jQuery .change() 未触发

标签 javascript jquery

只是在一些小页面上工作,作为一个更大项目的元素,我对我当前的问题完全感到困惑,这是我的代码:

<script src="https://ajax.googleapis.com/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>

但是无论浏览器如何,更改事件都不会触发,我也尝试过使用绑定(bind),但它让我陷入困境!

最佳答案

jQuery attribute selectors不需要 @ 前缀(如 XPath)。像这样更改您的代码:

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

Here是一个工作版本。

关于javascript - jQuery .change() 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5193445/

相关文章:

javascript - 将备用页面内容加载到 div onload 中

jquery - 一键显示响应式菜单,而不是两次

javascript - angularjs ng-repeat在选择选项中过滤多个字段

javascript - Vue 2.0 中监听多个 keyup 事件

javascript - PhantomJS - 从许多 URL 获取屏幕截图

jquery - Wordpress 中的视差滚动 Sprite

javascript - Inject.js 未在页面上加载?

javascript - 调整具有多种字体大小的 div 中的文本大小?

javascript - 这个 JSON 字符串有什么问题?

javascript - 通过输出的 document.write 设置文本样式