简体   繁体   English

ASP.Net Javascript集成

[英]ASP.Net Javascript integration

I am trying to use the following script in asp.net: 我正在尝试在asp.net中使用以下脚本:

<script language="javascript" type="text/javascript">
    function checktext() {
        var txt = document.getElementById('tbComments');

        if (txt.Text.Length > 0) {
            alert('Thank you for submitting feedback.');

            return true;
        }
        else {
            alert('Sorry, you must enter text before submitting.')

            return false;
        }
    }
</script>


    <asp:Button ID="btnSave" runat="server" Text="Submit" onclick="btnSave_Click" OnClientClick="checktext();" />

I have tried using it on the onclick event.. the script will just not work at all. 我尝试在onclick事件上使用它。该脚本根本无法使用。

Any Ideas? 有任何想法吗?

Try calling it like this: 尝试这样称呼它:

<asp:Button 
    ID="btnSave" 
    runat="server" 
    Text="Submit" 
    onclick="btnSave_Click" 
    OnClientClick="return checktext();" />

Also this line looks suspicious in a web forms application: 此外,此行在Web窗体应用程序中看起来也很可疑:

document.getElementById('tbComments');

Make sure that the generated id of your control is not prefixed with something else. 确保控件的生成ID没有其他前缀。

Replace: 更换:

<asp:Button ID="btnSave" runat="server" Text="Submit" onclick="btnSave_Click" OnClientClick="checktext();" />

with: 与:

<asp:Button ID="btnSave" runat="server" Text="Submit" onclick="btnSave_Click" OnClientClick="return checktext();" />

Replace: 更换:

var txt = document.getElementById('tbComments');

With: 带有:

var txt = document.getElementById('<%= tbComments.ClientId %>');

HTH. HTH。

you can use the ClientID property for getting the name of a Control on the client side. 您可以使用ClientID属性获取客户端上控件的名称。 I suggest you to try jQuery for all these though 我建议您尽管尝试所有这些的jQuery

var txt = document.getElementById('<%=tbComments.ClientID%>');

besides, the OnClientClick has to receive a true or false value, in order to "know" whether to send the request to the server; 此外,OnClientClick必须接收一个true或false值,以便“知道”是否将请求发送到服务器; so you have to change it with something like OnClientClick="return checktext();" 因此您必须使用OnClientClick="return checktext();"进行更改

everone else mentioned OnClientClick so I won't address that. 其他人都提到过OnClientClick,所以我不会解决。

assuming tbComments is a textbox of some kind, this line 假设tbComments是某种文本框,此行

if (txt.Text.Length > 0) {

is going to fail because Text is not a property of html inputs or textareas, which is how asp.net textboxes are rendered. 将失败,因为Text不是html输入或textareas的属性,而asp.net文本框的呈现方式则如此。 what you want is 你想要的是

if (txt.value.length > 0) {

also, is there some reason you're not using a regular asp.net RequiredFieldValidator control? 另外,是否由于某些原因您没有使用常规的asp.net RequiredFieldValidator控件? you're doing more work than you need to. 您要做的工作超出了您的需要。 If you absolutely have to have alert boxes, you can use a CustomValidator control to call your function (you'll have to tweak it to fit the model). 如果绝对必须有警报框,则可以使用CustomValidator控件来调用函数(必须对其进行调整以适合模型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM