如何禁用所有字段的表单提交,但提交按钮(How to disable form submit for all fields but the submit button)

编程入门 行业动态 更新时间:2024-10-17 07:28:36
如何禁用所有字段的表单提交,但提交按钮(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> 
  
 

更多推荐

本文发布于:2023-08-07 11:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464493.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   按钮   disable   button

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!