簡體   English   中英

根據下拉列表的選擇提交來自不同領域的數據 - laravel

[英]Submitting data from different fields depending on selection of dropdown - laravel

我的表單包含一個下拉菜單,可以選擇是或否。 如果選擇是,將出現一個下拉列表。 When no is selected an input text filed will appear. 但是,此功能正在運行,但我無法提交數據。 我只能在刪除“下拉列表 2”或“輸入 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>

解決方案是在字段隱藏時禁用字段。 因此,您可以使用

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

這里有一個解決類似問題的鏈接

此外,如果您的字段顯示為必填項,請檢查您的控制器的驗證。

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('');
    }      

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM