关于单选按钮的更改事件的jQuery

时间:2016-01-13 10:50:27

标签: javascript jquery html radio-button

这是我的HTML:

<div class="form-item">
 <label>Disability: </label>
  <div class="form-radios student_course_disablity">
    <div class="form-item" id="edit-student-course-disability-not-stated-wrapper">
     <label class="option" for="edit-student-course-disability-not-stated">
      <input type="radio" id="edit-student-course-disability-not-stated" name="student[course][disability]" value="not_stated" checked="checked" class="form-radio student_course_disablity">  Not Stated</label>
    </div>
 <div class="form-item" id="edit-student-course-disability-yes-wrapper">
   <label class="option" for="edit-student-course-disability-yes">
     <input type="radio" id="edit-student-course-disability-yes" name="student[course][disability]" value="yes" class="form-radio student_course_disablity"> Yes</label>
 </div>
 <div class="form-item" id="edit-student-course-disability-no-wrapper">
   <label class="option" for="edit-student-course-disability-no">
     <input type="radio" id="edit-student-course-disability-no" name="student[course][disability]" value="no" class="form-radio student_course_disablity"> No</label>
 </div>
</div>

我想为此单选按钮运行onchange事件:

我尝试过这种方法:

$('input[type=radio][name=student[course][disability]]').change(function() {
  alert('hi');
});

但没有运气

2 个答案:

答案 0 :(得分:3)

将名称放在双引号中,如下所示。希望这会有所帮助。

$('input[type=radio][name="student[course][disability]"]').change(function () {   
    alert('hi');
});

答案 1 :(得分:0)

最好不要使用class而不是使用name选择器进行分组,而且没有"引号。您可能想要使用:'input[name="student[course][disability]"]'

$('input.student-course-disability').on("change click", function() {
  alert('hi');
});

添加click事件,以防万一。不要忘记将class提供给<input />元素。

我已经为您提供了最佳解决方案,但是对于您的更新解决方案:

$('input[name="student[course][disability]"]').change(function () {   
    alert('hi');
});