2

如果输入无效,我会尝试显示警报,但 OnClientClick 不起作用,这是我的代码:

function validation1() {

    if (document.getElementById('firstname').value == "" || document.getElementById('firstname').value == "First Name..." ){
    alert("Please Enter First Name");
    } 
    if(document.getElementById('lastname').value == "" || document.getElementById('lastname').value == "Last Name..."){
    alert("Please Enter Last Name");
    }
}


<asp:Button runat="server" ID="SearchButton" Text="Instant Search" OnClick="SearchButton_Click" OnClientClick="validation1" />

我的代码在母版页中,所以这可能是个问题吗?

尝试了许多不同的方法来使用 OnClientClick 调用该函数,但没有成功:

4

2 回答 2

3

您在函数调用中缺少括号(括号),您可能还希望验证函数的结果确定服务器端点击是否应该触发:

function validation1() {

if (document.getElementById('firstname').value == "" ||document.getElementById('firstname').value == "First Name..." ){
   alert("Please Enter First Name");
   return false;
} 

if(document.getElementById('lastname').value == "" || document.getElementById('lastname').value == "Last Name..."){
   alert("Please Enter Last Name");
   return false;
}

return true;
}

<asp:Button runat="server" ID="SearchButton" Text="Instant Search" OnClick="SearchButton_Click" OnClientClick="return validation1();" />

这里的主要区别是客户端点击函数返回一个值来说明验证是否通过以及正确声明的实际方法调用,很容易错过:)。

于 2013-01-08T12:35:01.787 回答
0

试试这个

    function validation1() {

    if (document.getElementById('firstname').value == "" || document.getElementById('firstname').value == "First Name..." ){
    alert("Please Enter First Name");
    return false;
    } 
    if(document.getElementById('lastname').value == "" || document.getElementById('lastname').value == "Last Name..."){
    alert("Please Enter Last Name");
    return false;
    }
}

这是aspx代码

<asp:Button runat="server" ID="SearchButton" Text="Instant Search" OnClick="SearchButton_Click" OnClientClick="return validation1();" />
于 2013-01-08T12:42:22.837 回答