亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

尋找動態添加/刪除框但隱藏下拉值上的字段

尋找動態添加/刪除框但隱藏下拉值上的字段

喵喵時光機 2023-07-20 10:23:13
我希望使用 jQuery Ajax 動態地執行添加/刪除選擇框字段之類的操作,但并非所有文本框都會顯示,具體取決于動態下拉值是什么。我在網上找到了很多可以動態添加/刪除的腳本,但沒有任何基于下拉值的腳本。在下圖中,如果從下拉列表中選擇 Bags,則 Item Name 框將隱藏。<html> <head>  <title></title>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body>  <div class="container">   <form method="post" id="insert_form">    <div class="table-repsonsive">     <span id="error"></span>     <table class="table table-bordered" id="item_table">      <tr>       <th>Unit</th>       <th>Name</th>       <th>Quantity</th>       <th><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>      </tr>     </table>     <div align="center">      <input type="submit" name="submit" class="btn btn-info" value="Insert" />     </div>    </div>   </form>  </div> </body></html><script>$(document).ready(function(){ $(document).on('click', '.add', function(){  var html = '';  html += '<tr>';    html += '<td><select name="item_unit[]" class="form-control item_unit">';            html += '<option value="">Select Unit</option><?php echo numopt(); ?>';        html += '</select></td>';    html += '<td><input type="text" name="item_name[]"" class="form-control item_name" /></td>';    html += '<td><input type="text" name="item_quantity[]"" class="form-control item_quantity" /></td>';    html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';  $('#item_table').append(html);});    
查看完整描述

2 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

您可以僅禁用下拉列表中選擇的某個值的項目名稱輸入字段。您可以通過為下拉列表中的所選項目添加事件偵聽器來實現,并根據值禁用相應的輸入字段。



查看完整回答
反對 回復 2023-07-20
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

我建議像這樣禁用它:

  • 如果選擇輸入等于“Bags”

  • 選擇最接近的“項目名稱”輸入并將其隱藏

根據你的代碼

let restricts = ["bags", "kg"];


function hideQuantity(e) {

    if (restricts.indexOf(e.target.value) > -1) {

        e.target.closest("tr").querySelector(".item_quantity").setAttribute("disabled", "disabled");

    } else {

        e.target.closest("tr").querySelector(".item_quantity").removeAttribute("disabled", "disabled");

    }

}


$(document).ready(function () {

    $(document).on('click', '.add', function () {

        var html = '';

        html += '<tr>';

        html += '<td><select onclick="hideQuantity(event)" name="item_unit[]" class="form-control item_unit">';

        html += '<option value="">Select Unit</option><option value="bags">Bags</option><option value="inch">Inch</option><option value="kg">Kg</option><?php echo numopt(); ?>';

        html += '</select></td>';

        html += '<td><input type="text" name="item_name[]"" class="form-control item_name" /></td>';

        html += '<td><input type="text" name="item_quantity[]"" class="form-control item_quantity" /></td>';

        html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';

        $('#item_table').append(html);

    });

...

更多解釋:

首先,我添加了一些靜態數據用于測試:


html += '<option value="">Select Unit</option><option value="bags">Bags</option><option value="inch">Inch</option><option value="kg">Kg</option><?php echo numopt(); ?>';

其次,創建一個restricts數組。item_unit該數組保存您想要隱藏輸入的位置的值item_quantity:


let restricts = ["bags", "kg"]; // add as many as you want

第三,我創建了函數,該函數的目的是如果選擇的輸入值與數組的任何其他值匹配,hideQuantity則隱藏item_quantity輸入:item_unitrestricts


function hideQuantity(e) {

    if (restricts.indexOf(e.target.value) > -1) {

        e.target.closest("tr").querySelector(".item_quantity").setAttribute("disabled", "disabled");

    } else {

        e.target.closest("tr").querySelector(".item_quantity").removeAttribute("disabled", "disabled");

    }

}

最后,hideQuantity為每個item_unit選擇輸入添加函數:


html += '<td><select onclick="hideQuantity(event)" name="item_unit[]" class="form-control item_unit">';



查看完整回答
反對 回復 2023-07-20
  • 2 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號