【问题标题】:Submitting data from different fields depending on selection of dropdown - laravel根据下拉列表的选择从不同字段提交数据 - laravel
【发布时间】:2019-12-30 12:32:31
【问题描述】:

我的表单包含一个下拉菜单,可以选择是或否。如果选择是,将出现一个下拉菜单。选择否时,将出现输入文本字段。但是,此功能正在运行,但我无法提交我的数据。我只能在“Dropdown 2”或“input 1”之一被删除时提交我的数据(请参阅我的意思是哪个字段的代码)。

如果我提交数据,我会收到以下错误 htmlspecialchars() expects parameter 1 to be string, array given 。我认为这是因为我有两个可以发送数据的字段。这两个字段(隐藏和显示字段)然后作为数组发送数据,而不是从显示的字段向控制器发送数据。这怎么能解决。有人可以帮我吗?

谢谢

function displayField() {
      if (document.getElementById("isYes").selected) {
        document.getElementById("ifyes").style.display = "block";
        document.getElementById("check").style.display = "block";
        document.getElementById("ifno").style.display = "none";
        document.getElementById("ifyes").required = true;
      } else if (document.getElementById("isNo").selected) {
        document.getElementById("ifno").style.display = "block";
        document.getElementById("check").style.display = "block";
        document.getElementById("ifyes").style.display = "none";
        document.getElementById("ifno").required = true;
      } else {
        document.getElementById("ifno").style.display = "none";
        document.getElementById("ifyes").style.display = "none";
        document.getElementById("check").style.display = "none";
      }
    };
<form action="/tests" method="POST">

<div class="row">
  <div class="col-2">
    <div class="d-flex">
      <div class="p-1">
        <label class="p-2" for="type">Type </label>
      </div>
    </div>
  </div>
  <div class="col-4">
    <div class="d-flex">
      <div class="flex-fill p-2">
        <select name="type" class="form-control" onchange="displayField()" required>
          <option></option>
          <option id="isYes">Yes</option>
          <option id="isNo">No</option>
        </select>
      </div>
    </div>
  </div>

  <div class="col-2">
    <div class="d-flex">
      <div class="p-1">
        <label id="check" style="display: none;" class="p-2" for="select">Select</label>
      </div>
    </div>
  </div>
  <div class="col-4">
    <div class="d-flex">
      <div class="flex-fill p-2">
# Dropdown 2
        <select name="select" class="form-control" id="ifyes" style="display: none;" >


          <option>IC</option>
          <option>PD</option>
          <option>RB</option>
          <option>VDQ</option>
          <option>VDS</option>
        </select>
# input 1
        <input style="display: none;" id="ifno" type="text" class="form-control input-text" placeholder="select" name="select">
      </div>
    </div>
  </div>
</div> 
<div class="row">
  <div class="col-4 offset-4">
    <button type="submit" class="btn btn-primary btn-block" style="margin: 10px;">New </button>
  </div>
</div>  
</form>

【问题讨论】:

  • 做一件事。准备好文件后,按要求填写所有字段,以便用户在未填写必填字段的情况下无法提交。现在选择时,该选项会从其他隐藏字段中删除 required 并在可见字段中设置为 required。
  • 感谢您提供所需的解决方案。但是我该如何解决我得到的错误htmlspecialchars() expects parameter 1 to be string, array given

标签: javascript jquery html jquery-selectors dropdown


【解决方案1】:

解决方案是在字段被隐藏时禁用字段。因此,您可以使用

document.getElementById('id_of_field_to_disable').disabled = true

这里a link有类似问题的解决方案。

此外,如果您的字段是必需的,请签入控制器的验证。

【讨论】:

    【解决方案2】:
    I have also the same problem that form is not submit. but using below code 
    this works well for me.You should change according to your code.    
    <!-- This is my html code -->
     <select id="imA" name="imA" onchange="addfield();" required="">
                    <option value="">I am a ...</option>
                    <option value="teacher">Teacher</option>
                    <option value="student">Student</option>
     </select>
    
     <div id="showField">
     </div>
    
     <script type="text/javascript">
    function addfield(){
        var evalue = $('#imA').val();
        if(evalue == 'student'){            
            $('#showField').html('<input type="text" placeholder="  Enrollment 
     No." name="eno" id="eno" required>');
        }
        else{
            $('#showField').html('');
        }      
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多