LinkButton的OnClientClick事件 调用带参数的JS函数 参数未定义?
<head runat="server">
<title></title>
<script>
function set(obj) {
obj.text = "abc";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButton1" OnClientClick="set(TextBox1)" runat="server">LinkButton</asp:LinkButton>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
浏览器端:
<script>
function set(obj) {
obj.text = "abc";
}
</script>
。。。
<div>
<a onclick="set(TextBox1);" id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton</a>
<input name="TextBox1" type="text" id="TextBox1" />
</div>
错误: “TextBox1”未定义
[解决办法]
javascript中要调用 getElementById 函数。
[解决办法]
obj.text = "abc"改成obj.value = 'abc'
[解决办法]
<html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server"> <title></title> <script type="text/javascript"> function set(obj) { document.getElementById(obj).value = "abc"; } </script></head><body> <form id="form1" runat="server"> <div> <asp:LinkButton ID="LinkButton1" OnClientClick="javascript:set('TextBox1')" runat="server">LinkButton</asp:LinkButton> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> </form></body>
[解决办法]
javascript:set('TextBox1') 'TextBox1'是一个值。
javascript:set(TextBox1) 是一个变量。
[解决办法]
<script type="text/javascript">
function set(obj) {
document.getElementById(obj).value = "abc";
}
</script>
<asp:LinkButton ID="LinkButton1" OnClientClick="set('TextBox1')" runat="server">LinkButton</asp:LinkButton>
<asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox>