首页 新闻 会员 周边

ASP.NET Button同时执行2个脚本

0
悬赏园豆:10 [已解决问题] 解决于 2012-01-13 16:20

</head>
 
<script type="text/javascript">

//输入帐号后按回车切换焦点
function movefocus(){
    if(window.event.keyCode==13)
     {
      window.event.keyCode=9;
     }
}
//检验表单的合法性
function SuperLogin() {
 if (document.LoginForm.TextBox1.value == "") {
 alert('OnClientClick');

  alert("\请输入您的账号!");
  document.LoginForm.TextBox1.focus();
 }
 //else if (document.LoginForm.Password.value == "") {
  //alert("\请输入您的密码!");
 // document.LoginForm.Password.focus();
 //}
 else {
        return true;
    }
    return false;
}
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="SuperLogin();" OnClick="Button1_Click" />&nbsp;<asp:TextBox ID="TextBox1"
            runat="server"></asp:TextBox></div>
    </form>
</body>

 

为什么不能运行脚本呢?

飞翔的蚂蚁的主页 飞翔的蚂蚁 | 初学一级 | 园豆:131
提问于:2012-01-06 21:50
< >
分享
最佳答案
0

代码中document.LoginForm.TextBox1是找不到这个控件的,因为你下面只有名叫form1的form,没有名叫LoginForm的form.

这个javascript方法可以这样写啊。

 //检验表单的合法性
function SuperLogin() {

if (document.getElementById("TextBox1").value == "") {
alert('OnClientClick');
alert("\请输入您的账号!");
document.getElementById("TextBox1").focus();
}
else {
return true;
}
return false;
}

并且假如你执行了javascript方法返回false不执行后台代码的话,这句要加一个return,如下:

OnClientClick="return SuperLogin();"
收获园豆:10
LCM | 大侠五级 |园豆:6876 | 2012-01-06 22:41
其他回答(2)
0

TextBox1不是dom id,是asp.net web control id,所以document.getElementById("TextBox1")是没法找到textbox元素的,应该使用clientId

document.getElementById("<%=TextBox1.ClientID%>")

参考http://www.4guysfromrolla.com/articles/031710-1.aspx

klice | 园豆:173 (初学一级) | 2012-01-07 15:23
0

获取文本框里的内容是这样写的:document.getElementById("textBox的id");

因为你的textBox是服务器控件所以它的id会被编译所以js里要用生成以后的id

然后你调用的地方有个小问题上边也有人说了就是缺一个return; 应该这样写OnClientClick="return SuperLogin();"

迷恋郭德纲 | 园豆:67 (初学一级) | 2012-01-10 16:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册