简体   繁体   English

使用HTML5表单验证,仅当特定 <option>从一个<select>菜单被选中?

[英]Using HTML5 form validation, can I make an input field required only if a specific <option> from a <select> menu is selected?

Here's a simple form I have: 这是我的一个简单表格:

<form name="form_1">
  <fieldset>
    <select required name="choice">
      <option value="1">Choice 1</option>
      <option value="2">Choice 2</option>
      <option value="Other">Other</option>
    </select>
    <label for="other_text">If other, please specify: </label>
    <input id="other_text" name="other_text" size="30">
  </fieldset>
</form>

How do I make the "other_text" input field required if, and only if, the "choice" selection is set to value "Other" ? 当且仅当"choice"选项设置为值"Other"时,如何使"other_text"输入字段为required By default, the input field should not be required. 默认情况下,不需要输入字段。 Could this be done with only HTML5, or is Javascript required? 可以仅使用HTML5完成此操作,还是需要Javascript?

Since this use case is dependent upon user interaction and selection of an appropriate menu item, it requires Javascript, and can't be achieved independently with HTML5. 由于此用例取决于用户的交互作用和对适当菜单项的选择,因此它需要Javascript,并且不能单独使用HTML5实现。 You may use following snippet for the requested scenario 您可以针对请求的场景使用以下代码段

<form name="form_1">
    <fieldset>
        <select required name="choice">
          <option value="1">Choice 1</option>
          <option value="2">Choice 2</option>
          <option value="Other">Other</option>
        </select>
        <label for="other_text">If other, please specify: </label>
        <input id="other_text" name="other_text" size="30">
      </fieldset>
</form>
<script type="text/javascript">
    var form = document.getElementsByTagName('form')[0];
    form.onsubmit = function(e) {
        if (document.getElementsByTagName('select')[0].value == "Other") {
            e.preventDefault();
            // Block submitting form if option =  Other selected 
            document.getElementById('other_text').setAttribute('required', true);
            return false;
        }
        // Allow form submit otherwise
        document.getElementById('other_text').removeAttribute('required');
        console.log("Submitting form", e);
        return true;
    }
</script>

I believe that you must use a scripting language like javascript for this. 我相信您必须为此使用像javascript这样的脚本语言。 Give your select field an ID and call your javascript funciton 给您的选择字段一个ID,然后调用您的JavaScript函数

<form name="form_1">
 <fieldset>
  <select required name="choice" id="choice" onchange="changeAttribute()">
   <option value="1">Choice 1</option>
   <option value="2">Choice 2</option>
   <option value="Other">Other</option>
  </select>
  <label for="other_text">If other, please specify: </label>
  <input id="other_text" name="other_text" size="30">
 </fieldset>
</form>

Then use javascript to manipulate the other field 然后使用javascript操作其他字段

function changeAttribute(){
    if(document.getElementById('choice').value == "Other") {
        document.getElementById('other_text').setAttribute("required","");
    }else{
        document.getElementById('other_text').removeAttribute("required","");
    }
}   

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

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