简体   繁体   English

禁用表单提交按钮,直到已填充文本区域?

[英]disable a form submit button until a textarea has been populated?

can someone please help because i have tried various javascripts to get my form submit button to stay disabled until a user enters text into the textarea but nothings working. 有人可以帮忙吗,因为我尝试了各种JavaScript来使我的表单提交按钮保持禁用状态,直到用户在textarea中输入文本,但无济于事。

i want the submit button to be disabled until a user enters some text. 我希望禁用提交按钮,直到用户输入一些文本为止。 any suggestions please? 有什么建议吗?

  <form action="includes/welcomebio.php" method="post" id="form12" class="form12">          
 <textarea id="bio" textarea name="bio" data-id="bio" placeholder="Hi, my name is Peter. I'm 22 years old from North Wales." onKeyUp="checkWordCount();" data-required="true"><?php echo htmlspecialchars($profile['bio']); ?></textarea>
<input type="submit" class="welcome-submit" name="submit" value="Next ->" id="submit"/>
</form>

    <script type="text/javascript"> 
function disable() 
{ 
if(document.textarea.bio.value=="") 
{ 
document.textarea.submit.disabled=true; 
} 
else 
{ 
document.textarea.submit.disabled=false;    
} 
} 
</script> 

There are many things wrong with your code: 您的代码有很多错误:

  1. Try not to use inline javascript 尽量不要使用内联JavaScript
  2. your textarea onKeyUp calls a function that does not exist 您的textarea onKeyUp调用了一个不存在的函数
  3. you are trying to set the disabled state of the wrong element (you actually have invalid javascript) 您正在尝试设置错误元素的禁用状态(您实际上有无效的javascript)
  4. you have some invalid html too 您也有一些无效的html

This is what you want: 这就是你想要的:

HTML 的HTML

<form action="includes/welcomebio.php" method="post" id="form12" class="form12">
    <textarea id="bio" name="bio" data-id="bio" data-required="true" placeholder="Hi, my name is Peter. I'm 22 years old from North Wales.">
        <?php echo htmlspecialchars($profile['bio']); ?>
    </textarea>
    <input type="submit" class="welcome-submit" name="submit" value="Next ->" id="submit" />
</form>

JAVASCRIPT JAVASCRIPT

window.onload = function () {
    document.getElementById("bio").onkeyup = checkWordCount;
    checkWordCount();
};

function checkWordCount() {
    if (document.getElementById("bio").value == "") {
        document.getElementById("submit").disabled = true;
    } else {
        document.getElementById("submit").disabled = false;
    }
}

Here is a working example 这是一个有效的例子

You are trying to disable the textarea instead of the submit button. 您试图禁用textarea而不是Submit按钮。 Your code isn't valid JavaScript. 您的代码不是有效的JavaScript。 Keep the submit button disabled initially and enable it only when the textarea has something in it. 最初使提交按钮保持禁用状态,仅在文本区域中包含某些内容时才启用它。 Also, since you're using a placeholder for the textarea, your textarea would never be empty, so a check for 另外,由于您在文本区域中使用了占位符,因此您的文本区域永远不会为空,因此请检查

document.getElementById("bio").value = ""

would always return false unless the user changes it. 除非用户更改,否则始终返回false。

try this 尝试这个

     <body onload="disable()">
 <form action="includes/welcomebio.php" method="post" id="form12" class="form12">          
 <textarea id="bio" textarea name="bio" data-id="bio" placeholder="Hi, my name is Peter. I'm 22 years old from North Wales." onKeyUp="disable();checkWordCount();" data-required="true"></textarea>
<input type="submit" class="welcome-submit" name="submit" value="Next ->" id="submit"/>
</form>
</body>
    <script type="text/javascript"> 
function disable() 
{ 
    if(document.getElementById("bio").value=="") 
    { 
    document.getElementById("submit").disabled=true; 
    } 
    else 
    { 
    document.getElementById("submit").disabled=false;    
    } 
} 
</script> 

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

相关问题 我如何使用jQuery禁用表单的“提交”按钮,直到每个必填字段都已填写? - How do I use jQuery to disable a form's submit button until every required field has been filled? 禁用按钮提交,直到选择了下拉列表 - Disable button submit until dropdownlist been selected Jquery如何禁用提交按钮,直到选中单选按钮和复选框 - Jquery how to disable submit button until radio button and checkbox has been checked 禁用表单提交按钮,直到填充所有输入/文本区域字段,而不使用 for 循环(无 jQuery) - Disable form submit button until all input/textarea fields are filled, without using for loop (no jQuery) 禁用提交按钮,直到textarea超过100个字符 - Disable submit button until textarea ihas more than 100 chars 禁用/启用提交按钮,直到填写完所有表格 - Disable/Enable Submit Button until all forms have been filled 禁用表单提交按钮,直到未填写必填字段 - Disable form submit button untill required field is not populated 禁用表单提交,直到使用jQuery验证字段 - Disable Form Submit until Fields have been validated using jQuery 如果除一个以外的所有输入和文本区域均为空,请禁用表单提交按钮 - Disable form submit button if all input and textarea are empty except one 禁用表单字段,直到前一个字段已被JavaScript填充 - Disable form fields until a previous field has been filled with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM