单击时,Jquery 会多次显示警报消息
alert message displays multiple time by Jquery on click
我的警报信息显示三次甚至 4 次无法识别问题,但我知道我需要取消绑定,但无处可放。任何帮助将不胜感激。谢谢
jQuery('#compleave,#prevleave,#sickleave,#vacation').on('click', function(event) {
jQuery('#show2').show();
$("#but").remove();
if (this.id == 'prevleave') {
document.getElementById("ldays").value = but1_val2;
document.getElementById("ltype").value = "Privilege Leave";
}
else if (this.id == 'compleave') {
document.getElementById("ldays").value = but1_val1;
document.getElementById("ltype").value = "Dubai Casual Leave";
}
else if (this.id == 'sickleave') {
document.getElementById("ldays").value = but_val3;
document.getElementById("ltype").value = "Sickness";
}
else if (this.id == 'vacation') {
document.getElementById("ldays").value = but_val4;
document.getElementById("ltype").value = "Vacation";
}
$("#apply").click(funcapply);
});
function funcapply()
{
alert("HELLO CHECK THIS");
}
<div id="but">
<input type="button" id="prevleave" class="appButton1" value="Privilege Leave"> <br> <br>
<input type="button" id="compleave" class="appButton1" value="Dubai Casual Leave"> <br> <br>
<input type="button" id="sickleave" class="appButton1" value="Sickness"> <br> <br>
<input type="button" id="vacation" class="appButton1" value="Vacation"> <br> <br>
</div>
<div id="show2" class="container1" style="display:none;">
<label>LEAVE DAYS: </label> <input type="text" id="ldays"><br><br>
<input type="submit" class="applleave" value="APPLY LEAVE" id="apply" onclick="funcapply();">
</div>
你调用了两次 funcapply 函数。只需将输入的 html 替换为
<input type="submit" class="applleave" value="APPLY LEAVE" id="apply"/>
意味着您需要从中删除 OnClick 事件,因为它已被使用
$("#apply").click(funcapply);
将$("#apply").click(funcapply);
从 onClick 事件移动到
$(document).ready(function() {
$("#apply").click(funcapply);
})
只有在标记准备好接收事件后,这才会接收点击事件,即在您的情况下点击事件。
JSFiddle
试试这个,
$("#apply").click(funcapply); // when click on apply leaves
jQuery('#compleave,#prevleave,#sickleave,#vacation').click(function(event) {
jQuery('#show2').show();
$("#but").remove();
if (this.id == 'prevleave') {
document.getElementById("ldays").value = but1_val2;
document.getElementById("ltype").value = "Privilege Leave";
} else if (this.id == 'compleave') {
document.getElementById("ldays").value = but1_val1;
document.getElementById("ltype").value = "Dubai Casual Leave";
} else if (this.id == 'sickleave') {
document.getElementById("ldays").value = but_val3;
document.getElementById("ltype").value = "Sickness";
} else if (this.id == 'vacation') {
document.getElementById("ldays").value = but_val4;
document.getElementById("ltype").value = "Vacation";
}
});
function funcapply() {
alert("HELLO CHECK THIS");
}
运行演示
相关文章:
- 如果下载不成功,如何显示消息
- 如何在 jquery 中的速率按钮下方显示消息状态
- 删除所有可拖动元素后如何显示消息
- php表单验证n在表单上显示消息3秒钟并淡出
- 单击“点赞”按钮时显示消息
- javascript初学者-无法在屏幕上显示消息框
- 在表单提交后显示消息的方法
- 遍历列表,并在jQuery为空时显示消息
- 在jquery幻灯片登录表单上显示消息
- 如果文本筛选器在ng重复中未返回结果,则显示消息
- 以javascript显示消息框
- 在angular 1中,我如何检测angular是否受支持,并在屏幕上显示消息's不受支持
- 浏览器全屏显示,按键时不显示消息
- 选取随机数组元素,将其移除,并在没有更多可用元素时显示消息
- 仅当浏览器不是Google Chrome时显示消息
- 如果所有李都被隐藏,则显示消息
- 在提交表单和显示消息之前,如何检查表单输入是否全是数字
- 如果使用 JavaScript 登录失败,如何在登录页面中显示消息
- j查询验证未显示消息
- 为什么不显示消息