我想在单击按钮时从代码隐藏调用多个 javascript 函数.它适用于 3 个 JavaScript 函数,但不超过此
本文关键字:函数 javascript 适用于 JavaScript 不超过 按钮 单击 调用 隐藏 代码 | 更新日期: 2023-09-27 17:56:32
我需要从页面的代码隐藏页面调用 4 个 java 脚本函数 asp.net 它适用于 3 个 javascript 函数,但如果我添加第 4 个,这是一个验证函数。验证函数有效,但其他 3 个无效。
这行得通!
btnSubmit.OnClientClick = "return Getprodsize('" + hdnprdsize.ClientID + "');return formatSpecifications('" + hdnSpecifications.ClientID + "');return Getselectedvalue('" + hdndrop.ClientID + "')";
这行不通...
btnSubmit.OnClientClick = "return Validate();Getprodsize('" + hdnprdsize.ClientID + "');return formatSpecifications('" + hdnSpecifications.ClientID + "');return Getselectedvalue('" + hdndrop.ClientID + "')";
function Validate() {
var existing = $("#ctl00_contentMain_lblProductleft").text();
if ($('#ctl00_contentMain_txtProductName').val() == '') {
$('#ctl00_contentMain_txtProductName').focus().css("border-color", "red");
return false;
}
if ($('#ctl00_contentMain_Txtbrandname').val() == '') {
$('#ctl00_contentMain_Txtbrandname').focus().css("border-color", "red");
return false;
}
if ($('#ctl00_contentMain_Txtstock').val() == '') {
$('#ctl00_contentMain_Txtstock').focus().css("border-color", "red");
return false;
}
if ($('#ctl00_contentMain_Txtprice').val() == '') {
$('#ctl00_contentMain_Txtprice').focus().css("border-color", "red");
return false;
}
if ($('#ctl00_contentMain_txtShortDescription').val() == '') {
$('#ctl00_contentMain_txtShortDescription').focus().css("border-color", "red");
return false;
}
if ($('#ctl00_contentMain_txtLongDescription').val() == '') {
$('#ctl00_contentMain_txtLongDescription').focus().css("border-color", "red");
return false;
}
if ($('#ctl00_contentMain_ddlbcatgory option:selected').val() == 0) {
alert("Please Select Catagory");
return false;
}
if ($('#ctl00_contentMain_txtdeleverycharge').val() == 0) {
$('#ctl00_contentMain_txtdeleverycharge').focus().css("border-color", "red");
return false;
}
var txval = $('input[name=optradio]:checked').val();
$('#<%=hdnTax.ClientID%>').val(txval);
return true;
}
onClientClick
属性中不能有多个return
语句。第一个将返回,其余的不会被执行。您应该在不使用 return
的情况下调用其他函数,然后从 Validate()
函数返回值。
btnSubmit.OnClientClick = "Getprodsize('" + hdnprdsize.ClientID + "'); formatSpecifications('" + hdnSpecifications.ClientID + "'); Getselectedvalue('" + hdndrop.ClientID + "'); return Validate();";
如果要先检查验证,并且在验证失败时不调用其他函数,请使用if
btnSubmit.OnClientClick = "if(Validate()) {Getprodsize('" + hdnprdsize.ClientID + "'); formatSpecifications('" + hdnSpecifications.ClientID + "'); Getselectedvalue('" + hdndrop.ClientID + "'); return true;} else return false";
如果您将所有这些逻辑移到一个新函数中,并在 OnClientClick
中调用该函数,那会更容易。
function validateAndFormat(sizeID, specID, dropID) {
if(Validate()) {
Getprodsize(sizeID);
formatSpecifications(specID);
Getselectedvalue(dropID);
return true;
} else {
return false
}
}
btnSubmit.OnClientClick = "validateAndFormat('" + hdnprdsize.ClientID + "', '" + hdnSpecifications.ClientID + "' + "', '" + hdndrop.ClientID + "');"