我想为页面加载函数中的按钮分配一个单击处理程序。然后,我想使用以下代码片段稍后处理按钮的实际单击:
$("#finish").off('click').on('click', function () {
var sku = $("#productSKU").val();
var name = $("#productName").val();
var affiliateId = $("#AffiliateID").val();
var Errors = "";
var category = null;
var defaultName = $('#defaultImgName').val();
if ($("#childCategories_" + categoryLevel).length == 0) {
category = $("#categoryList").val();
}
else if ($("#childCategories_" + categoryLevel).val() == 0) {
category = null;
}
else {
category = $("#childCategories_" + categoryLevel).val();
}
if (!sku) {
Errors += "<li> You can not add a product without an Item Code</li>";
}
if (!name) {
Errors += "<li> You can not add a product without a Name</li>";
}
if (!category) {
Errors += "<li> You can not add a product without a Category</li>";
}
if (Errors != "") {
cua({ text: Errors, type: 'danger' })
}
else {
data.formData = { productName: name, productSKU: sku, affiliateID: affiliateId, categoryID: category, defaultImgName: defaultName }
data.submit();
}
});
如何在页面加载时分配点击事件处理程序,然后稍后触发实际的点击事件?
原因是如果未上传图像,则会导致按钮无响应,我发现这是由事件处理问题引起的。
最佳答案
$(document).ready(function(){
$("#finish").on('click', yourfunctionhere)
});
您可以将代码放在单独的函数中,因此如果您想将其关闭,然后再次打开,只需 1 行代码即可完成。
$("#finish").on('click', yourfunctionhere)
要执行该函数,您可以触发点击(当处理程序打开时):
$("#finish").trigger('click');
(感谢@putvande提醒)
关于javascript - jQuery 在页面加载时分配单击事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37114483/