如何在页面加载时禁用 .change 功能
How to disable .change function when the page loads
在我的$(document).ready()
函数中,我有一个绑定到集合或单选按钮的.change
事件。但是,如果用户提交的表单上出现错误,页面将使用用户选择的值重新加载。这导致单选按钮.change
函数在每次页面加载时触发,我不希望它这样做。我将如何禁用此功能?
编辑
这是完整的功能,由于现在中间很混乱,因此之前没有发布,并试图牢记这篇文章的可读性。
仅供参考 - 如果用户提交表单后出现错误,那么我需要页面像用户点击提交按钮时一样加载表单。由于我们根据单选按钮选择显示一组特定的div
和span
元素,因此我使用了trigger('change')
方法。有没有办法检测操作是否在页面加载期间发生?
$('input[name=TransactionType]').change(function (e) {
//Clear any messages from previous transaction
$('.error').text('');
$('.success').text('');
//Display fieldset Submit and Cancel(back to search) link
$('#PageDirection').show();
$('#AgentInformation').show();
//Display input fields
var radioValue = $(this);
if (radioValue.attr('id') == "Enroll" || (radioValue.attr('id') == "ModT")) {
//Enable and disable input boxes
$('span').not('#AllInput').hide();
$('#AllInput').show();
$('#PayFrequency').show();
$('#refNumberInput').show();
//Set tooltips
$('#AccountType').removeAttr("title");
if (radioValue.attr('id') == "Enroll") {
$('#PaymentFrequency').removeAttr("title");
$('.req').show();
$('#refNumberInput').show();
$('#enrollNote').show();
} else {
$('#PaperStatement').show();
$('#PaymentFrequency').attr("title", "Select the frequency to pay the distributor");
$('#refNumberInput').show();
}
} else if (radioValue.attr('id') == "New") {
$('span').not('#NewMessage').hide();
$('#NewMessage').show();
$('.req').show();
$('#refNumberInput').show();
} else if (radioValue.attr('id') == "ModA") {
$('#AllInput').show();
$('#AgentIdSpan').show();
$('.req').show();
$('#UnenrollMessage').hide();
$('#NewMessage').hide();
$('#PayFrequency').hide();
$('#PaperStatement').hide();
$('#refNumberInput').show();
$('#AgentId').attr("title", "Enter the Agent ID to be modified");
} else if (radioValue.attr('id') == "Clone") {
$('span').not('#AgentIdSpan').hide();
$('#AgentIdSpan').show();
$('.req').show();
$('#AgentId').attr("title", "Clone EFT onto this Agent ID");
} else if (radioValue.attr('id') == "Unenroll") {
$('span').not('#UnenrollMessage').hide();
$('#UnenrollMessage').show();
$('.req').show();
$('#refNumberInput').show();
}
if (radioValue.attr('id') != "Clone") {
$('.alwaysReq').show();
};
}).filter(':checked').trigger('change');
如果您不关心自动完成,则可以将autocomplete="off"
放在form
上。您还可以将当前字段值与其默认值进行比较,以查看它是否由浏览器自动填充。
你可以在jquery ready函数中使用"on"或"delegate"。喜欢这个:
$(document).delegate("input[name=TransactionType]","change", function(){
$('.error').text('');
$('.success').text('');
});
document.ready
通常不会调用更改函数。仅当您手动更改复选框或受其他函数影响的相同事件时,才会调用它,但不会在 document.ready
中调用。
请删除.trigger('change')
方法,因为它会在document.ready
上触发更改方法
相关文章:
- 添加文字和评论功能更新Div
- JavaScript打印功能使日历停止工作
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何防止网页加载后自动启动功能
- 除修剪外的其他功能
- 悬停功能触发器
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 删除CKEditor工具栏按钮,但不删除功能
- 异步facebook功能
- Selenium WebDriver and JavaScript change
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- jQuery滚动功能只工作一次
- Graphiti中是否有任何工具提示功能
- React redux初始化功能,无论状态变化如何
- Bootstrap 3 Datepicker v4:dp.show和dp.change事件在Datepicker内联时不
- 在哪里可以学习ECMAScript标准中尚未包含的JavaScript功能
- .change()不会'使用自动完成功能时无法在Chrome中工作
- 如何在页面加载时禁用 .change 功能
- jquery移动功能on.change只在某些时候起作用
- jquery .change功能使脚本在IE中崩溃