如何在 ASP.NET 页面加载(更新/更改)时调用javascript(或jQuery)
How to call javascript (or jQuery) on page load (update/change) in ASP.NET
我有创建某个对象的页面。在此页面上,如果用户选中复选框,将显示带有一些文本字段的隐藏区域。然后,当用户尝试提交对象时,服务器可以发送验证错误,因此用户需要更正某些字段。问题是,当发生这种情况时,具有其他字段的区域将再次隐藏(但是复选框被选中,因此用户需要取消选中,然后再次选中)。那么是否可以在每次发生更改或服务器向我发送错误时调用此函数?为了显示和隐藏其他部分,我正在使用以下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();
});
但是这些功能根本不起作用。我应该说页面是部分的,正文标签在共享布局中,所以我不确定在 onload 中调用此类函数对于正文标签或类似的东西是否是一种好习惯......
加载部分页面时,您可以使用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);
}
}
相关文章:
- DropDownListFor赢得't在更改时调用函数
- ng更改函数未被调用,并且没有错误
- 如何使用异步调用更改工厂的变量
- 是否可以在不更改上下文的情况下调用函数.apply
- 为什么(如何)'这'从函数内部调用回调时发生更改
- Angular:如何在状态更改时调用scope方法
- 由控制器调用的服务更改时,Angular未更新视图
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- 更改函数内部的全局变量而不调用它
- 更改事件绑定's函数在页面加载时调用
- 每次数据表中的页面更改时调用一个函数
- 更改HTML按钮值的颜色,其中值来自调用Javascript的onclick
- 如何调用更改事件,例如在 HTML 数据列表上选择
- 更改调用函数时选择框的选项值
- 未对输入更改调用指令
- PHP:更改调用脚本的环境
- 当选择框更改调用控制器方法时
- 查询ajax-根据ajax响应更改调用函数的返回值
- ajax加载的内容更改调用它的文档中的元素
- 从asp.net下拉列表更改调用jquery模式弹出