如何使用 jquery 在单选按钮上应用更改事件
How to apply on change event on radio button using jquery
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>
如何在 <input type="radio" name="data[Customer][service_type]" id="service_type" value="cancel">
上应用更改事件?
$(function(){
$("input:radio[name='data[Customer][service_type]']").change(function(){
var _val = $(this).val();
console.log(_val);
});
});
.change
是您需要触发的事件。
使用 on change
事件。
$("[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>
我正在使用此代码
<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);
}
我已经尝试了以上所有代码,但对我来说,动态生成单选按钮时下面的代码有效。
$(document).ready(function () {
$(document).on("change","input[name='data[Customer][service_type]']" ,function() {
console.log("radio changed");
});
});
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>
如何在 <input type="radio" name="data[Customer][service_type]" id="service_type" value="cancel">
上应用更改事件?
$(function(){
$("input:radio[name='data[Customer][service_type]']").change(function(){
var _val = $(this).val();
console.log(_val);
});
});
.change
是您需要触发的事件。
使用 on change
事件。
$("[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>
我正在使用此代码
<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);
}
我已经尝试了以上所有代码,但对我来说,动态生成单选按钮时下面的代码有效。
$(document).ready(function () {
$(document).on("change","input[name='data[Customer][service_type]']" ,function() {
console.log("radio changed");
});
});