[英]How to select dynamic class in jQuery change function?
I have a input field having dynamic class as:我有一个输入字段,其动态类为:
<input class="chek" data-id="1" type="checkbox" data-toggle="toggle">
<input class="chek" data-id="2" type="checkbox" data-toggle="toggle">
<input class="chek" data-id="3" type="checkbox" data-toggle="toggle">
Here is my Js:这是我的 Js:
$(".chek").unbind().change(function(e) {
var getid = $(this).attr('data-id');
alert(getid);
e.preventDefault();
$.ajax({
url: base_url + '/admin/program/setslider?selected='+getid,
type: 'POST',
dataType: 'json',
data: getid,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
Problem is $(this).attr('data-id');
问题是
$(this).attr('data-id');
returns all the data-id attr refering to same class chek (i supposed $(this)
would return data-id if specific class only but it returns all the data-id)返回所有引用同一个类 chek 的数据 ID 属性(我认为
$(this)
仅在特定类时返回数据 ID,但它返回所有数据 ID)
Solution I suppose is getting dynamic class for selecting specific input like:我想解决方案是获取用于选择特定输入的动态类,例如:
<input class="chek_1" data-id="1" type="checkbox" data-toggle="toggle">
<input class="chek_2" data-id="2" type="checkbox" data-toggle="toggle">
<input class="chek_3" data-id="3" type="checkbox" data-toggle="toggle">
And JS be like:而 JS 就像:
$(".chek_'+ id +'").unbind().change(function(e) {
// code here
}
I am using bootstrap toggle http://www.bootstraptoggle.com/ to make change in checkbox.我正在使用引导程序切换http://www.bootstraptoggle.com/在复选框中进行更改。
I think you want to get the change event of the checkboxes.我认为您想获取复选框的更改事件。
Based on your HTML you can use the following code to get the change event.根据您的 HTML,您可以使用以下代码来获取更改事件。
$("input[data-toggle='toggle']").change(function(){
alert(this.checked);
});
OR或者
$("input:checkbox").change(function(){
alert(this.checked);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.