如何根据另一个动态下拉列表和文本框为动态文本框指定默认值?

我正在寻求执行以下操作。尝试让数量列文本框具有基于同一动态表中的下拉列表和文本框的默认值。

因此,在下图中,当单位下拉列表设置为 2,然后在第 2 行中输入数量为 5 时,下面的任何行都将具有第 2 行的默认数量。因此第 6 行和第 7 行的默认值为 5。

https://img2.mukewang.com/64c4c1110001ce3f11570469.jpg

<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>

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");

    }

}


墨色风雨
浏览 100回答 1
1回答

陪伴而非守候

提供两种方式的更新。我希望它能像您预期的结果一样工作(但仍然像我在评论中建议的那样,建议考虑使用不“设计相关”的技术来管理此问题的另一种方法。例如 Javascript 对象或 LocalStorage...)let restricts = ["bags", "kg"];function hideQuantity(e) {&nbsp; &nbsp; if (restricts.indexOf(e.target.value) > -1) {&nbsp; &nbsp; &nbsp; &nbsp; e.target.closest("tr").querySelector(".item_quantity").setAttribute("disabled", "disabled");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; e.target.closest("tr").querySelector(".item_quantity").removeAttribute("disabled", "disabled");&nbsp; &nbsp; }}$(document).ready(function(){&nbsp; &nbsp; $(document).on('click', '.add', function () {&nbsp; &nbsp; &nbsp; &nbsp; var html = '';&nbsp; &nbsp; &nbsp; &nbsp; html += '<tr>';&nbsp; &nbsp; &nbsp; &nbsp; html += '<td><select onclick="hideQuantity(event)" name="item_unit[]" id="item_unit" class="form-control item_unit">';&nbsp; &nbsp; &nbsp; &nbsp; html += '<option value="">Select Unit</option><option value="bags">Bags</option><option value="inch">Inch</option><option value="kg">Kg</option><option value="2">2</option><option value="3">3</option><option value="4">4</option>';&nbsp; &nbsp; &nbsp; &nbsp; html += '</select></td>';&nbsp; &nbsp; &nbsp; &nbsp; html += '<td><input type="text" name="item_name[]"" class="form-control item_name" /></td>';&nbsp; &nbsp; &nbsp; &nbsp; html += '<td><input type="text" name="item_quantity[]"" class="form-control item_quantity" /></td>';&nbsp; &nbsp; &nbsp; &nbsp; html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';&nbsp; &nbsp; &nbsp; &nbsp; $('#item_table').append(html);&nbsp; &nbsp; });&nbsp;$(document).on('click', '.remove', function(){&nbsp; $(this).closest('tr').remove();&nbsp;});&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $(document).on('change keyup focusout','.item_quantity',function(){&nbsp; &nbsp; &nbsp; &nbsp; //I called it in french to avoid confusion with value val...&nbsp; &nbsp; &nbsp; &nbsp; var valeur =$(this).val();&nbsp; &nbsp; &nbsp; &nbsp; var unitreference= $(this).parents("tr").find("td:first select.item_unit").val();&nbsp; &nbsp; &nbsp; &nbsp; $('.item_unit').each(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($(this).val()==unitreference ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).parents("tr").find("td:eq(2) input.item_quantity").val(valeur).attr('disabled', 'disabled');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; $(this).removeAttr("disabled");&nbsp; &nbsp; })&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $(document).on('change','.item_unit',function(){&nbsp; &nbsp; &nbsp; &nbsp; //Update using the unit selector&nbsp; &nbsp; &nbsp; &nbsp; var selectedUnit =$(this).val();&nbsp; &nbsp; &nbsp; &nbsp; //Here we look for the Quantity field being used as Referece&nbsp; &nbsp; &nbsp; &nbsp; var quantRef= $('select option[value='+selectedUnit+']:selected').parents("tr").find("td:eq(2) input.item_quantity:not(:disabled)").val()&nbsp; &nbsp; &nbsp; &nbsp; //console.log(selectedUnit)&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //console.log(quantRef)&nbsp; &nbsp; &nbsp; &nbsp; if(quantRef){&nbsp; &nbsp; &nbsp; &nbsp; $('.item_unit').each(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($(this).val()==selectedUnit ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).parents("tr").find("td:eq(2) input.item_quantity").val(quantRef).attr('disabled', 'disabled');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp;&nbsp;&nbsp;$('#insert_form').on('submit', function(event){&nbsp; event.preventDefault();&nbsp; var error = '';&nbsp; $('.item_name').each(function(){&nbsp; &nbsp;var count = 1;&nbsp; &nbsp;if($(this).val() == '') {&nbsp; &nbsp; error += "<p>Enter Item Name at "+count+" Row</p>";&nbsp; &nbsp; return false;&nbsp; &nbsp;}&nbsp; &nbsp;count = count + 1;&nbsp; });&nbsp;&nbsp;&nbsp; $('.item_quantity').each(function(){&nbsp; &nbsp;var count = 1;&nbsp; &nbsp;if($(this).val() == '') {&nbsp; &nbsp; error += "<p>Enter Item Quantity at "+count+" Row</p>";&nbsp; &nbsp; return false;&nbsp; &nbsp;}&nbsp; &nbsp;count = count + 1;&nbsp; });&nbsp;&nbsp;&nbsp; $('.item_unit').each(function(){&nbsp; &nbsp;var count = 1;&nbsp; &nbsp;if($(this).val() == '') {&nbsp; &nbsp; error += "<p>Select Unit at "+count+" Row</p>";&nbsp; &nbsp; return false;&nbsp; &nbsp;}&nbsp; &nbsp;count = count + 1;&nbsp; });&nbsp; &nbsp; &nbsp;&nbsp; var form_data = $(this).serialize();&nbsp; if(error == '') {&nbsp; &nbsp;$.ajax({&nbsp; &nbsp; url:"insert.php",&nbsp; &nbsp; method:"POST",&nbsp; &nbsp; data:form_data,&nbsp; &nbsp; success:function(data) {&nbsp; &nbsp; &nbsp;if(data == 'ok') {&nbsp; &nbsp; &nbsp; $('#item_table').find("tr:gt(0)").remove();&nbsp; &nbsp; &nbsp; $('#error').html('<div class="alert alert-success">Item Details Saved</div>');&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp;});&nbsp; } else {&nbsp; &nbsp;$('#error').html('<div class="alert alert-danger">'+error+'</div>');&nbsp; }&nbsp;});});<html>&nbsp;<head>&nbsp; <title></title>&nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>&nbsp; <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />&nbsp; <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>&nbsp;</head>&nbsp;<body>&nbsp; <div class="container">&nbsp; &nbsp;<form method="post" id="insert_form">&nbsp; &nbsp; <div class="table-repsonsive">&nbsp; &nbsp; &nbsp;<span id="error"></span>&nbsp; &nbsp; &nbsp;<table class="table table-bordered" id="item_table">&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp;<th>Unit</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Quantity</th>&nbsp; &nbsp; &nbsp; &nbsp;<th><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp;</table>&nbsp; &nbsp; &nbsp;<div align="center">&nbsp; &nbsp; &nbsp; <input type="submit" name="submit" class="btn btn-info" value="Insert" />&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; </div>&nbsp; &nbsp;</form>&nbsp; </div></body></html>PS:抱歉我很忙:) 一个建议:如果可能的话,您可以在单元格表上添加一些类,以便您可以使用closest() 优化您的代码。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript