如何使用jQuery在单选按钮上应用change事件

5

HTML:

<div class="radio-list  ">
     <label class="radio-inline">
     <span class=""><input type="radio" name="data[Customer][service_type]" id="service_type" value="new" checked=""></span> NEW INSTALLATION </label>
     <label class="radio-inline">
     <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="repair"></span> SERVICE REPAIR </label>
  <label class="radio-inline">
  <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="cancel"></span> CANCEL SERVICE </label>
  <label class="radio-inline">
  <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="other"></span> OTHER SERVICE </label>
</div>

如何在上应用change事件?
4个回答

8
$(function(){

    $("input:radio[name='data[Customer][service_type]']").change(function(){
        var _val = $(this).val();
        console.log(_val);
    });

});

.change 是您需要触发的事件。

在这里查看。


1
使用 onchange 事件。

$("[name='data[Customer][service_type]']").on("change", function (e) {
    console.log(this.value);
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="radio-list  ">
    <label class="radio-inline"> <span class=""><input type="radio" name="data[Customer][service_type]" id="service_type" value="new" checked=""></span> NEW INSTALLATION</label>
    <label class="radio-inline"> <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="repair"></span> SERVICE REPAIR</label>
    <label class="radio-inline"> <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="cancel"></span> CANCEL SERVICE</label>
    <label class="radio-inline"> <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="other"></span> OTHER SERVICE</label>
</div>


0

我尝试了上面的所有代码,但在动态生成单选按钮时,下面的代码是有效的。

$(document).ready(function () {
 
$(document).on("change","input[name='data[Customer][service_type]']" ,function() {
      console.log("radio changed");
  });
  });

0

我正在使用这段代码

<div class="radio-list  ">
     <label class="radio-inline">
     <span class=""><input type="radio" name="data[Customer][service_type]" id="service_type" value="new" checked="" onclick="myFunction(this.value)"></span> NEW INSTALLATION </label>

</div>

和 JavaScript 函数代码

function myFunction(myval)
{
alert(myval);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接