散分30.net使用Javascript判断Textbox点提交按钮时不可为空!顶者有分!
判断代码:
<script language= "javascript " type= "text/javascript ">
function Op()
{
if((document.getElementById( 'Op_yj ').value.length==0))
{
alert( '请填写您的意见及建议! ');
return false;
}
}
</script>
HTML页面提交按钮源代码:
<td align= "center " width= "70 " background= "../images/hq3_bg1.gif " height= "29 "> <asp:button id= "Op_tj " runat= "server " Text= "提 交 " OnClick= "return Op(); "> </asp:button> </td>
这样对吗????请高手指点一二!!!
------解决方案--------------------不行
服务器端怎能触发客户端事件
------解决方案--------------------onclientclick
------解决方案-------------------- <asp:button id= "Op_tj " runat= "server " Text= "提 交 " OnClientClick= "return Op(); ">
你用的vs2003还是2005?2005下面button有onclientclick事件,直接返回客户端事件,2003就要用别的方法了
------解决方案-------------------- <td align= "center " width= "70 " background= "../images/hq3_bg1.gif " height= "29 "> <asp:button id= "Op_tj " runat= "server " Text= "提 交 " OnClientClick= "return Op(); "> </asp:button> </td>
------解决方案--------------------也可以,不过可以判断一下是否只有空格等情况replace(/(^\s+)|(\s+$)/g, " ").length
可以写成
if((document.getElementById( 'Op_yj ').value.replace(/(^\s+)|(\s+$)/g, " ").length==0))
------解决方案--------------------不可以这样写。
<td align= "center " width= "70 " background= "../images/hq3_bg1.gif " height= "29 "> <asp:button id= "Op_tj " runat= "server " Text= "提 交 " OnClick= "return Op(); "> </asp:button> </td>
不能在服务器控件上这样触发事件。 可以这样写看看:
在后台的 Page_Load中写。
this.Op_tj.Attributes.Add( "onClick ", "Op(); ");
------解决方案--------------------vs2003
在page_load中加入
this.Button1.Attrubutes[ "onclick "] = "return Op(); ";
vs2002005
<asp:button id= "Op_tj " runat= "server " Text= "提 交 " OnClientClick= "return Op(); "> </asp:button>
JS中直接
if(document.getElementById( 'Op_yj ').value == " ")
{
// to do:
}
------解决方案--------------------先写一个Js脚本判断函数
function chkForm()
{
with(document.all)
{
if(item( "文本框的ID ").value== " ")
{
alert( "不能为空! ");
item( "文本框的ID ").focus();
return false;
}
}
}
<asp:Button id= "btnAdd " runat= "server " Text= "提交 " OnClientClick= "return chkForm() "> </asp:Button>
------解决方案--------------------