【发布时间】:2019-07-18 22:21:09
【问题描述】:
我打开了一个带有 2 个文本框和一个按钮的 jquery 模态对话框
<table cellpadding="0" cellspacing="0">
<tr>
<td align="center"><input name="" type="text" value="" title="User Name" class="width190 enter_popup" id="txtUserName" onfocus="txtFocus(this)" onblur="txtFBlur(this,'0')"/></td>
<td align="center"><input name="" type="password" value="" title="Password" class="width190 enter_popup" id="txtPassword" onfocus="txtFocus(this)" onblur="txtFBlur(this,'1')"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="" type="submit" class="blue_btn" value="Sign In" id="btnLogIn" onclick="javascript:return LogIn()"/></td>
</tr>
</table>
现在在我的脚本中我正在调用 LogIn 函数
$(function () {
$(".enter_popup").keypress(function (e) {
if (e.keyCode == 13) {
if ($(this).attr('id') == "txtUserName" || $(this).attr('id') == "txtPassword") {
LogIn();
}
}
});
});
function LogIn() {
var username = $('#txtUserName').val();
var password = $('#txtPassword').val();
}
但是按键没有被触发..任何想法为什么以及可能的解决方案是什么?
【问题讨论】:
-
在这里工作正常jsfiddle.net/dzxu1c8s 所以我猜该元素是动态创建的,因此需要
event-delegation$(document).on('keypress','.enter_popup',function(){/*code*/}); -
还验证了您的代码正在运行。试试上面安东的解决方案。
-
@Anton:这个
$(document).on('keypress','.enter_popup',function(){/*code*/});为我工作.. 将其作为答案发布。
标签: javascript jquery asp.net-mvc-3 jquery-dialog