javascript - jquery 函数不调用按钮单击

标签 javascript jquery html

下面是我的代码,我只是想提醒“hi”和“hello”,以便我可以将实际代码放在那里。

<html>
<head>
<script type="text/javascript"  src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='jquery-1.11.1.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>

  <script src="jquery-ui.js"></script>
        <link rel="stylesheet" href="jquery-ui.css">
</head>

<script>

$(document).ready(function(){
    alert("hi");
    $('form[name="lgform"]').submit(function(evnt){
        evnt.preventDefault();
    });

    $("#btn_login").click(function(){       
        alert("hello");
    });

});
</script>

<body>
<form name="lgform">
<div>

<table id="table"  >
    <tr>

        <td width="35px"><input id="mob"  type="text" name="mob_nu"></td>

        <td width="35px"><input id="pswd"  type="password" name="pswd_vl"></td>

        <td width="100px"><input type="button" name="login_btn" id="btn_login" value="Login"></td>          
    </tr>

</table>
</div>
</form>

</body>
</html>

按钮单击事件不起作用,我在其他一些地方使用相同的 jquery 库,该库工作正常。我必须在这个页面中调用ajax,我需要调用按钮单击而不提交页面,因为我还有几个按钮要添加。任何一段代码都值得赞赏并提前致谢。

最佳答案

当我尝试你的代码时,我收到了这个错误

忽略了对“alert()”的调用。该文档已沙盒化,并且未设置“allow-modals”关键字

当我尝试用 console.log 替换警报后,它工作正常。

您的代码工作正常,请将警报替换为 console.log()

关于javascript - jquery 函数不调用按钮单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31593910/

相关文章:

html - 如何下载单个网页以供离线查看?

css - 居中 Canvas

javascript - 如何使用某个 src url 查找 img 并使用 vanilla javascript 更改 css?

javascript - var foo = function(){...}() 和 var foo = (function(){...}()) 之间的区别

JavaScript 正则表达式替换哈希

javascript - 如何访问放置在其旁边的元素?

jquery - MVC 贡献网格

javascript - 将 jQuery 代码转换为 Polymer?

javascript - Angular 无法实例化模块

javascript - 分屏滚动网站无法正常工作