.click() 不起作用。 jQuery

发布于 2024-12-10 03:13:54 字数 939 浏览 0 评论 0原文

在我的login.aspx 中,我尝试通过单击按钮执行简单的客户端验证(检查用户名和密码字段是否为空)。但是 jquery 中的 .click() 没有触发。我哪里错了。

注意:我有一个事件 LoginButton_Click 其背后的代码具有一些功能。

<script src="Scripts/Login.js" type="text/javascript"></script>

 <td align="right" colspan="2">
     <asp:Button ID="LoginButton" runat="server"  Text="Log In" 
       onclick="LoginButton_Click" class ="validateForm" />
</td>

我的login.js 文件:

$(function () {

  $('#LoginButton').click(function () {

            var username = $('#txtUserName');
            var pwd = $('#txtPassword');
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

        });
});

提前感谢

BB

In my login.aspx I am trying to perform simple client side validation (check Username and password fields are empty or not) on the click of the button. But the .click() in the jquery is not firing. Where am i going wrong.

Note : I have an event LoginButton_Click on the code behind which has some functionality.

<script src="Scripts/Login.js" type="text/javascript"></script>

 <td align="right" colspan="2">
     <asp:Button ID="LoginButton" runat="server"  Text="Log In" 
       onclick="LoginButton_Click" class ="validateForm" />
</td>

My login.js file :

$(function () {

  $('#LoginButton').click(function () {

            var username = $('#txtUserName');
            var pwd = $('#txtPassword');
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

        });
});

Thanks in advance

BB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丶情人眼里出诗心の 2024-12-17 03:13:54

$("#<%= LoginButton.ClientID %>") 不适用于外部 JavaScript 文件。如果要使用外部文件,请在 aspx 文件中创建一个全局变量并在 JavaScript 文件中访问它。


编辑:

在aspx文件中创建全局变量。

var loginButtonId = <%= LoginButton.ClientID %>;

在 JavaScript 文件中使用它

$("#" + loginButtonId ).click(function () {...});

注意: 变量应在添加引用之前声明到 JavaScript 文件。

$("#<%= LoginButton.ClientID %>") does not work on external JavaScript files. If you want to use an external file, create a global variable in the aspx file and access it in the JavaScript file.


Edit:

create global variable in aspx file.

var loginButtonId = <%= LoginButton.ClientID %>;

use it in the JavaScript File

$("#" + loginButtonId ).click(function () {...});

note: variable should declare before adding the reference to JavaScript file.

硬不硬你别怂 2024-12-17 03:13:54

这不会给你带来任何问题,使用 jquery 选择器,我们通过以 LoginButton 结尾的 id 属性来选择它。

$(function () {
  $("input[id$=LoginButton]").click(function () {

            var username = $("input[id$=txtUserName]");
            var pwd = $("input[id$=txtPassword]");
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

        });
});

This should not give you any problems, using jquery selectors we select it by its id attribute which ends with LoginButton.

$(function () {
  $("input[id$=LoginButton]").click(function () {

            var username = $("input[id$=txtUserName]");
            var pwd = $("input[id$=txtPassword]");
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

        });
});
迷爱 2024-12-17 03:13:54

因为在您的 .js 文件中,javascript 无法读取以下值:$("#<%= LoginButton.ClientID %>")
您需要:
- 将此代码放在 html 页面的末尾。

- 在js文件中放置一个固定的选择器

because in your .js file, javascript couldn't read the value of this: $("#<%= LoginButton.ClientID %>")
You need to:
- Put this code at the end of your html page.
OR
- Put a fixed selector in the js file

浅笑依然 2024-12-17 03:13:54

我想你想要onclientclick

http://msdn.microsoft.com /en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

以防万一有人关心...

<script src="Scripts/Login.js" type="text/javascript"></script>

 <td align="right" colspan="2">
     <asp:Button ID="LoginButton" runat="server"  Text="Log In" 
       onclick="LoginButton_Click" onclientclick="validateLogin()" class ="validateForm" />
</td>

login.js 文件:

function validateLogin() {

            var username = $('#txtUserName');
            var pwd = $('#txtPassword');
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

            return true;
}

I think you want onclientclick.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

Just in case someone out there cares...

<script src="Scripts/Login.js" type="text/javascript"></script>

 <td align="right" colspan="2">
     <asp:Button ID="LoginButton" runat="server"  Text="Log In" 
       onclick="LoginButton_Click" onclientclick="validateLogin()" class ="validateForm" />
</td>

login.js file :

function validateLogin() {

            var username = $('#txtUserName');
            var pwd = $('#txtPassword');
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

            return true;
}
咆哮 2024-12-17 03:13:54

#LoginButton 不存在。当 asp 按钮被呈现时,它将被呈现为与包含它的 ASP 控件相关的专用 ID,例如 ctrl00_mycontrol_panelid_LoginButton。为了将其引入外部文件,您需要做的是使用全局变量:

var loginButtonID;
var txtUserNameID;
var txtPasswordID;

$(function () {

  $(loginButtonID).click(function () {

            var username = $(txtUserNameID);
            var pwd = $(txtPasswordID);
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

        });
});

然后在包含此 login.js 文件的文件中,您需要通过调用来设置这些变量:

<script type="text/javascript">

loginButtonID = '<%=LoginButton.ClientID%>';
txtUserNameID = '<%=txtUserName.ClientID%>';
txtPasswordID = '<%=txtPassword.ClientID%>';

</script>

确保不要使用 new " 重新声明这些变量var”修饰符。这可能会破坏范围并搞砸你的脚本。

#LoginButton doesn't exist. When the asp button gets rendered it will get rendered into a specialized ID related to the ASP controls that contain it like ctrl00_mycontrol_panelid_LoginButton. What you need to do in order to bring this into an external file is use a global variable:

var loginButtonID;
var txtUserNameID;
var txtPasswordID;

$(function () {

  $(loginButtonID).click(function () {

            var username = $(txtUserNameID);
            var pwd = $(txtPasswordID);
            if (username.val().length <= 0) {
                alert("User Name is required.");
                return false;
            }
            if (pwd.val().length <= 0) {
                alert("Password is required.");
                return false;
            }

        });
});

Then in the file that includes this login.js file, you need to set these variables by calling:

<script type="text/javascript">

loginButtonID = '<%=LoginButton.ClientID%>';
txtUserNameID = '<%=txtUserName.ClientID%>';
txtPasswordID = '<%=txtPassword.ClientID%>';

</script>

Be sure not to redeclare these with new "var" modifiers. That may ruin the scope and screw up your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文