呵呵, 我有一个表单,有两个提交按钮:一个接受,一个取消:
<p>
<input type="submit" name="accio" value="accept" class="default" />
<input type="submit" name="accio" value="cancel" class="cancel" />
</p>
我想使用键盘使用 jQuery 提交表单:
$(document).ready(function() {
$("input[type=text]").focus();
$("input[type=text]").keydown(function(e) {
if ((e.keycode && e.keycode == 13) || (e.which && e.which == 13)) {
$(".default").submit();
}
if ((e.keycode && e.keycode == 27) || (e.which && e.which == 27)) {
$(".cancel").submit();
}
});
});
我希望参数 accio 根据按下的键包含接受或取消(我在服务器中处理它)...它在 Firefox 中有效,但在 IE 或 Chrome 中无效。我的错在哪里?
更新
按照@Sime,我得到了以下代码:
$(document).ready(function() {
$("input:text").focus();
$("input:text").keydown(function(e) {
if (e.which == 13) {
$("#default").submit();
}
if (e.which == 27) {
$("#cancel").submit();
}
});
});
但它仍然不起作用...我认为这是我提交表单的方式...我不能在“input:submit”上调用提交方法并期望将正确的值传递给 Controller ?
最佳答案
试试这个:
$("input[type=text]").keydown(function(e) {
if ((e.keycode && e.keycode == 13) || (e.which && e.which == 13)) {
$(".default")[0].click();
}
if ((e.keycode && e.keycode == 27) || (e.which && e.which == 27)) {
$(".cancel")[0].click();
}
});
你能提供一种更好地识别按钮的方法吗?
$("p input.cancel")
更新:
$(document).ready(function() {
$("input:text").focus();
$("input:text").keydown(function(e) {
if (e.which == 13) {
$("#default")[0].click();
}
if (e.which == 27) {
$("#cancel")[0].click();
}
});
});
更新二
此代码在 IE6+ 中运行良好,更正位于 stopPropagation
$('input[type="text"]').keydown(function(ev) {
if (ev.which == 13 || ev.which == 27) {
ev.stopPropagation();
if (ev.which == 13)
$("#default")[0].click();
if (ev.which == 27)
$("#cancel")[0].click();
return false
}
});
关于jquery - 通过 jQuery 键盘提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5235540/