如何在javascript中获取Textbox Id [英] How to get Textbox Id in javascript

查看:58
本文介绍了如何在javascript中获取Textbox Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中编写了以下函数并将其调用为按钮。

I have written the following function in javascript and call it to a button.

<script type="text/javascript">
       function CheckValidation() {
           var strMachineCode = document.getElementById('<%= txtMachineCode.ClientID %>').value;
           if (strMachineCode == "") {
               alert("Please provide Machine code");
           }

       }
   </script>

Call to server side button
<asp:Button ID="btnSave" runat="server" CssClass="button" Text="Save" 

            Width="80px" onclick="btnSave_Click" OnClientClick="CheckValidation();" />





但这样做不显示警报。在此先感谢。



But this does not show alert. Thanks in advance.

推荐答案

为什么要这样?

你所做的是运行OnClientClick,而不是去服务器,所以你的页面刷新和警告消息是无关紧要的!

如果你的JS代码应该验证页面,你应该通过添加返回false来停止传播服务器端事件; 警告消息后......
Why should it?
What you have done is running the OnClientClick, and than went to the server, so your page refresh and the alert message is irrelevant!
If your JS code should validate the page than you should stop the propagation of the server side event by adding return false; after the alert message...


这篇关于如何在javascript中获取Textbox Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆