简体   繁体   English

禁用提交按钮,直到textarea超过100个字符

[英]Disable submit button until textarea ihas more than 100 chars

How can I disable the submit button until the chars of a textarea are more than 100 chars? 如何在textarea的字符超过100个字符之前禁用提交按钮?

This is a code used to check if the user has selected to upload an image. 这是用于检查用户是否已选择上传图像的代码。 Please inform me how should I name my textarea and guide me through the installation of it. 请告诉我如何命名我的textarea并指导我完成安装。

$(function() {
   $('form').submit(function() {
      if(!$("form input[type=file]").val()) {
         alert('You must select a file!');
         return false;
      }
   });
});

First of, make the submit button disabled, eg. 首先,禁用提交按钮,例如。

<input type="submit" disabled="disabled" id="submitid" />

Next you should write a function that will count the length of the textarea while the user is writing, this could be done by using the keyup function in jQuery or onkeyup in plain Javascript. 接下来你应该编写一个函数,在用户编写时计算textarea的长度,这可以通过使用jQuery中的keyup函数或普通Javascript中的onkeyup来完成。 Example in jQuery: jQuery中的示例:

$("#textareaid").keyup(function () {
  if((this).val().length > 100) {
    $("#submitid").removeAttr('disabled');
  } else {
    $("#submitid").attr("disabled", "disabled");
  }
});

Note: code not tested. 注意:代码未经过测试。

SetInterval approach: SetInterval方法:

setInterval(function () {
  if($("#textareaId").val().length > 100) {
    $("#submitid").removeAttr("disabled");
  } else {
    $("#submitid").attr("disabled", "disabled");
  }
}, 500); //Runs every 0.5s

Full size example: 全尺寸示例:

<form>
  <textarea id="textareaId"></textarea>
  <input type="submit" id="submitId" disabled="disabled" />
</form>
<script type="text/javascript">
  setInterval(function () {
    if($("#textareaId").val().length > 100) {
      $("#submitId").removeAttr("disabled");
    } else {
      $("#submitId").attr("disabled", "disabled");
    }
  }, 500); //Runs every 0.5s
</script>

You can either update the status of the submit button every time the data within the text area is changed, however this can be problematic as each browser has different events for textareas (in javascript) 您可以在每次更改文本区域中的数据时更新提交按钮的状态,但这可能会有问题,因为每个浏览器都有不同的textareas事件(在javascript中)

A common way to do it is by setting an interval to check the submit box, then based on that decide the boolean value for the button. 一种常见的方法是设置一个间隔来检查提交框,然后根据它确定按钮的布尔值。 For instance 例如

window.setInterval(function(){ $('#submitBtn').disabled =  $("#textArea").val().length < 100 }, 100);

(Code not tested) (代码未经测试)

Users much prefer it if the number of characters in textarea is validated on submit. 如果在提交时验证textarea中的字符数,用户会更喜欢它。 If it's less than 100, cancel the submit and give the user an error message saying why. 如果小于100,则取消提交并向用户提供错误消息,说明原因。 Presumably there's a hint on the page so the user should know of the 100 character criterion and how many characters they've entered. 据推测,页面上有一个提示,因此用户应该知道100个字符标准以及他们输入的字符数。

If you really want the button disabled, then disable it using script when the page loads. 如果您确实要禁用该按钮,则在页面加载时使用脚本禁用它。 Use a key-up listener to count the characters in the textarea and enable the submit button if there's more than 100. You may need to listed to other events too so if the user changes the text by dragging or paste, then the submit button's disabled property is updated appropriately. 使用键盘监听器来计算textarea中的字符并启用提交按钮(如果超过100)。您可能还需要列出其他事件,如果用户通过拖动或粘贴更改文本,则提交按钮被禁用财产得到适当更新。

Building on Minitech's rather terse answer: 以Minitech相当简洁的答案为基础:

$(function() {
    $('form').submit(function() {
        if(!$("form input[type=file]").val()) {
            alert('You must select a file!');
            return false;
        }

        if($("#theTextArea").val().length <= 100) {
            alert("You must enter more than 100 characters!");
            return false;
        }
   });
});

Just replace theTextArea with the id of the specific textarea you want to test 只需将theTextArea替换为您要测试的特定textarea的id即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM