[英]passing a variable to javascript function from code behind
我想从后面的代码将字符串值传递给javascript函数。 因为这是我得到一个未被捕获的参考错误的时刻,解释该值未定义。
var variable= txtVariable.Value;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Registering", "RegisterTheUser("+variable+");", true);
建议也许是正确的语法
这是功能
function RegisterTheUser(val) {
alert(val);
}
问候
你发布的内容看起来很好。 我没有任何问题地使用了以下内容:
ScriptManager.RegisterStartupScript(this, typeof(string), "Registering", String.Format("RegisterTheUser('{0}');", myVariable), true);
你能尝试使用我发布的例子吗?
该问题可能是您RegisterStartupScript
正在加载功能之前执行RegisterTheUser
。
您需要整理脚本加载的顺序或添加一些逻辑,以便在document
准备好后调用此函数。
在jQuery中这样做:
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Registering", "$(document).ready(function(){ RegisterTheUser("+variable+"); });", true);
正如sbhomra在他的回答中所指出的,如果值是一个字符串,你可能需要添加单个qoutes。 如果它是一个数字,则不需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.