简体   繁体   English

如何多次从数据列表下拉列表中选择值,然后每次将值存储在表的新行中?

[英]How can i select values from datalist dropdown multiple times and store the value each time in a new row in table?

I am setting up a new web page where I want to select value from datalist dropdown and store it in a table row but when I select value from the same datalist second time the previous value gets replaced instead of that I want value selected second time from datalist to store in the second row of a table? 我正在设置一个新的网页,我想从数据列表下拉列表中选择值并将其存储在表行中,但是当我第二次从同一数据列表中选择值时,以前的值将被替换,而不是我希望第二次从中选择值数据列表存储在表的第二行? How can I achieve that? 我该如何实现?

I have tried to add value selected in a datalist in a table row which is adding in a table row but when I select a value from same datalist second time the previous value gets replaced but I want the second value to be stored in the second row of a table, how can I achieve this? 我试图添加在表行的数据列表中选择的值,该值正在表行中添加,但是当我第二次从同一数据列表中选择一个值时,以前的值被替换了,但我希望第二个值存储在第二行中一张桌子,我怎么能做到这一点?

<input list="browa" name="adv">

   <datalist id="browa">

          <option value="Twice a Day">
          <option value="Thrice a Day">
          <option value="After meal">
          <option value="Before meal">
          <option value="Once a Day">

   </datalist>  

  <input list="brown" name="dose">

      <datalist id="brown">

           <option value="Morning">
           <option value="Afternoon">
           <option value="Evening">
           <option value="Night">

      </datalist> 

</div>

 <table style="width:100%; margin-top:10px;">
    <tr>

       <th>Advice</th>

       <th>Dose</th>                                          
   </tr>

   <tr>

      <td id="adv"></td>
      <td id="dose"></td>

   </tr>

  </table>

<script src="//code.jquery.com/jquery.min.js"></script>
<script>

            $("input[name=adv]").on('change', function () {
                $("#adv").text($(this).val());
            });

            $("input[name=dose]").on('change', function () {
                $("#dose").text($(this).val());
            });
  </script>

As of now, I am getting a result like this ( https://ibb.co/2nfTV1N ) but I want results like this ( https://ibb.co/YLhCrgk ). 到目前为止,我得到的结果是这样的( https://ibb.co/2nfTV1N ),但是我想要这样的结果( https://ibb.co/YLhCrgk )。 please tell me how can I do this? 请告诉我该怎么做?

Yes the value is being replaced because you are just setting the .text property. 是的,该值已被替换,因为您只是在设置.text属性。 You should use .append or .add, or some method that doesn't overwrite. 您应该使用.append或.add或某种不会覆盖的方法。

If you want it to append an actual new grid row, then create a new element that is the grid row populated with the correct values. 如果希望它附加实际的新网格行,则创建一个新元素,该元素是填充了正确值的网格行。 Then, append the new element 然后,附加新元素

$("input[name=adv]").on('change', function() { $("#adv").append($("<tr><td></td></tr>") .text($(this).val())); });

暂无
暂无

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

相关问题 每当用户使用jQuery在选择下拉列表中选择特定值时,如何执行功能? - How can I perform a function each time that the user select a specific value into a select dropdown using jQuery? 如何多次从数组中随机选择一个对象并将每个对象存储在一个数组中? - How do I randomly select an object from an array multiple times and store each in an array? 我如何从表的每一行获取值并将其存储在js数组中 - How do i get the values from each row of a table and store them in a js array 从HTML表格删除一行后,如何存储单元格的值? - How can I store the value of a cell upon deleting a row from an HTML table? 我可以在React Storybook中创建一个旋钮来修改具有4个值的数组,其中每个值都有一个选择下拉列表吗? - Can I create a Knob in React Storybook to modify an array with 4 values where each value has a select dropdown? 我如何 select JavaScript 下拉列表中的第一个值 - How can I select the first value from dropdown in JavaScript 如何检查表格的每一行,选择 select 的值,而不是获取所有可用的选项和文本输入值 - How to check for each row of a table, the value of the selected select instead of getting all the available options and text input values 如何根据下拉菜单中的选择显示表格值? - How can I display a table value based on the selection from a dropdown? 如何从不同的数据列表中选择值,然后一次将其显示在同一文本区域框中? - How to select values from different datalist and display it in same textarea box at a time? 每行的 HTML 表格下拉值 - HTML table dropdown value for each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM