[英]change event using name attribute of checkbox JS
I have this multiple checkboxes with same name attribute: myTask
我有多个具有相同名称属性的复选框: myTask
<input type="checkbox" class="sol-checkbox" name="myTask" value="1B">
<input type="checkbox" class="sol-checkbox" name="myTask" value="2B">
<input type="checkbox" class="sol-checkbox" name="myTask" value="3B">
and和
I have this jQuery
code to perform every time the user clicks on the checkboxes每次用户单击复选框时,我都会执行此jQuery
代码
$("input[name='myTask'").change(function(){
alert(1);
});
is there anything wrong with my script?我的脚本有什么问题吗?
Please check may be something wrong in your code.请检查您的代码中可能有问题。 You have forgot to add one square bracket你忘了加一个方括号
$("input[name='myTask']").change(function(){ alert($(this).val()); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class="sol-checkbox" name="myTask" value="1B"> <input type="checkbox" class="sol-checkbox" name="myTask" value="2B"> <input type="checkbox" class="sol-checkbox" name="myTask" value="3B">
You forgot add one closing square bracket.您忘记添加一个右方括号。
Please try below code.请尝试以下代码。
Way to trigger the on change through the input type.通过输入类型触发 on 变化的方式。
// Shorthand for $( document ).ready()
$(function() {
console.log( "ready!" );
$("input[name='myTask']").change(function(){
alert($(this).val());
});
});
Way to trigger the on change through the class name of the input field.通过输入字段的 class 名称触发 on change 的方式。
// Shorthand for $( document ).ready()
$(function() {
console.log( "ready!" );
$(".sol-checkbox").change(function(){
alert($(this).val());
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.