我有一个使用onblur
和ondblclick
的文本框-双击它们会打开一个弹出屏幕,但我不希望触发onblur。
调用双击函数时,我删除了onblur属性以阻止其触发。可以,但是现在我试图在弹出窗口打开后重新添加onblur,但是它不起作用
function OpenCust(v) {
$('#<%= txtDebtor.ClientID %>').removeAttr('onblur');
shadowboxopen = true;
if (!v || 0 === v.length) {
}
else {
Shadowbox.open({
content: "lookups/Customer.aspx?NODEBT=true&CustomerAC=" + v,
player: "iframe",
onClose: function () { shadowboxopen = false; },
title: "Edit Customer"
});
}
$('#<%= txtDebtor.ClientID %>').attr('onblur');
}
编辑: 更改了代码,以启用和禁用模糊功能,但是在调用双击OpenCust时仍会触发onblur。
文本框:<asp:TextBox runat="server" ID="txtDebtor" onblur="CheckIfAccountCodeDebtValid(this.value)" ondblclick="OpenCust(this.value)"></asp:TextBox>
function OpenCust(v) {
$('#<%= txtDebtor.ClientID %>').off('blur', CheckIfAccountCodeDebtValid(v));
shadowboxopen = true;
if (!v || 0 === v.length) {
}
else {
Shadowbox.open({
content: "lookups/Customer.aspx?NODEBT=true&CustomerAC=" + v,
player: "iframe",
onClose: function () { shadowboxopen = false; },
title: "Edit Customer"
});
}
setTimeout(function() {
$('#<%= txtDebtor.ClientID %>').on('blur', CheckIfAccountCodeDebtValid(v));
}, 2000);
}
答案 0 :(得分:2)
重新添加时,必须指定onblur的值。 jQuery中执行此操作的正确函数是on()
和off()
。在下面的示例中,您可以看到如何单击按钮后生病,然后生病2秒钟后再次添加它。如果按钮在这2秒钟内失去焦点,则不会显示模糊控制台消息。如果它之后失去焦点。
//Add blur event handler to the button
$('#button').on('blur', blurFunction);
//Add click event handler to the button
$('#button').on('click', function() {
//Remove blur event handler
$('#button').off('blur', blurFunction);
console.log('click');
setTimeout(function() {
//reattach blur event handler after 2 seconds
$('#button').on('blur', blurFunction);
}, 2000);
});
//The actual blur event handler function
function blurFunction() {
console.log(this.value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="button" type="button" value="test-value">
使用使用参数的函数,您可以将其包装在匿名函数中,如下面的代码段所示。
//Add blur event handler to the button
$('#button').on('blur', function() {
CheckIfAccountCodeDebtValid(this.value);
});
//Add click event handler to the button
$('#button').on('click', function() {
//Remove all blur event handlers
$('#button').off('blur');
console.log('click');
setTimeout(function() {
//reattach blur event handler after 2 seconds
$('#button').on('blur', function() {
CheckIfAccountCodeDebtValid(this.value);
});
}, 2000);
});
//The actual blur event handler function
function CheckIfAccountCodeDebtValid(value) {
console.log(value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="button" type="button" value="test-value">