Javascript jQuery更改事件调用了两次
我有一个带有切换复选框的表单,问题是当我更改时,我提交表单两次 我不知道原因是什么 这是我的代码:Javascript jQuery更改事件调用了两次,javascript,jquery,switchery,Javascript,Jquery,Switchery,我有一个带有切换复选框的表单,问题是当我更改时,我提交表单两次 我不知道原因是什么 这是我的代码: $('.switchery').change(function(e) { //var isChecked = $(this).is(":checked"); //console.log('isChecked: ' + $(this).attr('name')); $.post('http://localhost/centrix/', {"x":"x"}, function(data)
$('.switchery').change(function(e) {
//var isChecked = $(this).is(":checked");
//console.log('isChecked: ' + $(this).attr('name'));
$.post('http://localhost/centrix/', {"x":"x"}, function(data){
// alert(1)
},"json");
});
我的HTML代码:
<div class="checkbox checkbox-switchery checkbox-right switchery-xs">
<label class="display-block">
<input type="checkbox" name="yes1" class="switchery" checked="checked">
Overdue
</label>
</div>
<div class="checkbox checkbox-switchery checkbox-right switchery-xs">
<label class="display-block">
<input type="checkbox" name="yes2" class="switchery">
Today
</label>
</div>
<div class="checkbox checkbox-switchery checkbox-right switchery-xs">
<label class="display-block">
<input type="checkbox" name="yes1" class="switchery" checked="checked">
Tomorrow
</label>
</div>
<div class="checkbox checkbox-switchery checkbox-right switchery-xs">
<label class="display-block">
<input type="checkbox" class="switchery" checked="checked">
Open
</label>
</div>
<div class="checkbox checkbox-switchery checkbox-right switchery-xs">
<label class="display-block">
<input type="checkbox" class="switchery">
Completed
</label>
</div>
逾期的
今天
明天
打开
完整的
$('.switchery')。单击(函数(e){
e、 停止即时复制()
…
请尝试以下操作
$(文档).ready(函数(){
$('.switchery')。打开(“单击touchend”,函数(e){
var isChecked=$(this).is(“:checked”);
console.log('isChecked:'+$(this.attr('name'));
});
});
逾期的
今天
明天
打开
完整的
首先,你使用一个插件,它是……只要提到它的链接就好了……不是每个人都能读懂你的心思(!)
然后,似乎应该将更改处理程序附加到插件创建的
,而不是
。就这么简单。别问我为什么!!但是很容易找到,看看用户真正点击的内容
你的问题转载于
解决方案是:
$(文档).ready(函数(){
//在每个。。。
$('.switchery')。每个(函数(){
新开关厂(本);
});
//更改处理程序。
$('span.switchery')。打开(“单击”,函数(e){
控制台日志(“YO”);
});
});
逾期的
今天
明天
打开
完整的
当您单击链接到交换机的标签时,不会触发“单击”事件提供的解决方案
这是我的解决方法:使用一个带有小延迟的setTimeout
$('.switchery').change(function(e) {
//clear the timeout (for the 2nd call)
clearTimeout($(this).data("timeoutChange"));
//set the timeout, and link it to your switch
$(this).data("timeoutChange", setTimeout(function(){
$.post('http://localhost/centrix/', {"x":"x"}, function(data){
// alert(1)
},"json");
}),100)
});
这个问题已经过去了几年,但是如果您在这里寻找答案,只需将
标记放在
标记之外。并且,不要忘记将for
属性添加到标签中,以引用相应的输入()
逾期的
这取决于代码的实际布局或使用方式。如果您选中给定的复选框,当前代码不会发送两次请求。请参阅,我会将其更改为“$”('[input type=“checkbox”]”)。在(“mousedown”,函数(e){”上,然后查询复选框的状态。同样的问题:(我们不做“已解决”)在此标题中解决的问题是标签、参考
<div class="checkbox checkbox-switchery checkbox-right switchery-xs">
<label class="display-block" for="yes1-input">Overdue</label>
<input type="checkbox" name="yes1" id="yes1-input" class="switchery" checked="checked">
</div>