我有用于创建某些对象的页面。如果用户选中了此复选框,则在此页面上将显示带有某些文本字段的隐藏区域。然后,当用户尝试提交对象时,服务器可以发送验证错误,因此用户将需要更正某些字段。问题是,发生这种情况时,具有其他字段的区域会再次被隐藏(但是选中了复选框,因此用户需要先取消选中然后再次选中它)。那么是否有可能在每次发生更改或服务器向我发送错误消息时调用此函数?为了显示和隐藏其他部分,我正在使用以下jQuery代码:
function changeVisibilityOfCreateCustomerSection() {
if ($("#isnewcustomer").prop("checked") == true) {
$(".saf-createcustomersection > div > input").prop("disabled", false);
$(".saf-createcustomersection").show();
} else {
$(".saf-createcustomersection > div > input").prop("disabled", true);
$(".saf-createcustomersection").hide();
}
}
我这样称呼它:
@Html.EditorFor(model => model.isNewCustomer,
new { htmlAttributes = new {
@class = "checkBox",
onchange = "changeVisibilityOfCreateCustomerSection();",
id = "isnewcustomer" } })
我试图在javascript标签中使用类似这样的内容:
$(document).ready(function () {
changeVisibilityOfCreateCustomerSection();
});
$(window).load(function () {
changeVisibilityOfCreateCustomerSection();
});
但是这些功能根本不起作用。我应该说页面是局部的并且body标签处于共享布局中,所以我不确定在onload中为body标签调用此类函数还是诸如此类的好习惯。
加载部分页面时,可以使用Page_Load执行以下操作:
protected void Page_Load(object sender, EventArgs e){
// checks here if the page is loaded normally not with a submission to the server from the same page
if(!IsPostBack){
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "functionName()", true);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句