如何禁用所有字段的表单提交,但提交按钮(How to disable form submit for all fields but the submit button)
如何禁用所有输入字段的表单提交,但是提交按钮(名为“提交”的ID)。 下面的代码有效,但问题是我不能在textareas中做新的行。 我正在使用jQuery。 我的目标是阻止输入提交表单。
$('form').on('keyup keypress', function(e) { var code; code = e.keyCode || e.which; if (code === 13) { e.preventDefault(); return false; } });How can I disable form submit for all input fields but the submit button (with id called submit). The below code works but the problem is that I cannot do new lines in textareas. I am using jQuery. My goal is to prevent enter from submitting form.
$('form').on('keyup keypress', function(e) { var code; code = e.keyCode || e.which; if (code === 13) { e.preventDefault(); return false; } });最满意答案
你可以检查输入textarea是否有焦点。
检查代码段
(function($) { $('form').on('keyup keypress', function(e) { if ($("input,textarea").is(":focus")) { //input and text area has focus } else { var code; code = e.keyCode || e.which; if (code === 13) { e.preventDefault(); console.log('hello'); return false; } } }); })(jQuery);<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action=""> <textarea name="user_eingabe" cols="50" rows="10"></textarea> <input type="submit"> </form>you can check if the input textarea has focus or not.
check code snippet
(function($) { $('form').on('keyup keypress', function(e) { if ($("input,textarea").is(":focus")) { //input and text area has focus } else { var code; code = e.keyCode || e.which; if (code === 13) { e.preventDefault(); console.log('hello'); return false; } } }); })(jQuery);<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action=""> <textarea name="user_eingabe" cols="50" rows="10"></textarea> <input type="submit"> </form>
更多推荐
发布评论