简体   繁体   English

动态创建输入字段时,<select> $.each 只填充一次,为什么?

[英]When dynamically creating input fields, <select> filled with $.each only once, why?

I try to dynamically add an input field to a form.我尝试将输入字段动态添加到表单中。 It works fine except one thing:它工作正常,除了一件事:

I try to fill a with $.each, but this only works for the first dynamically added if I add more, the stays empty..我尝试用 $.each 填充 a,但这仅适用于第一个动态添加的,如果我添加更多,则保持为空。

the #add button is in the initial form: #add 按钮处于初始形式:

append :附加

    $('#add').click(function() {
        i++;
        $dynamic_field').append('' +
        '<h1>Sensor '+i+'</h1>   ' +
        '<tr id="row'+i+'">
        '<td><div class="form-group">\n' +
                 '<label for="InputSensorType">Sensor Type*</label>\n' +
                  '<select class="form-control" id="InputSensorType" name="sensorType[]"></select>\n' 
         '</div></td>\n +'
         '<td>
              <button type="button" name="copy" id="copy" class="btn btn-primary">Copy this sensor
              </button></td><tr>');

Each:每个:

          var jsarray = <?php echo json_encode($sensors)?>;

           $.each(jsarray, function (index, value) {
               $('#InputSensorType').append(('<option value='+index+'>'+value+'</option>'));
           });

Can anyone help me?谁能帮我?

Try this尝试这个

var jsarray = '<?php echo json_encode($sensors)?>';
var obj = jQuery.parseJSON(jsarray);
var data = '';
 $.each(jsarray, function (index, value) {               
           data +='<option value='+index+'>'+value+'</option>';
            });
 $('#InputSensorType').append(data);

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

相关问题 如何一次 select 两个输入字段并检查它们是否已填写? - How to select two input fields at once and check if they are filled? 输入字段动态填充时如何启用提交按钮? - How to enable submit button when input fields are filled dynamically? 仅在填写输入字段时显示预加载器 gif? - Show preloader gif only when input fields are filled? 无法填写动态生成的输入字段 - Dynamically generated input fields cannot be filled 仅在填充所有输入字段后启用元素。 可重复使用 function - Only enable element once all input fields are filled. Reusable function CloneNode 如果输入的所有字段都填写了,但字段未填写时仍然克隆 - CloneNode if all the fields input is filled in still cloning when fields are not filled in 填充输入字段时更改按钮的颜色 - Change colour of button when input fields are filled 填写输入字段时应显示图像 - Image should show when input fields are filled in 仅当在别处填写多个独立输入字段时才取消隐藏外部按钮 - Unhide an external button only when multiple indipendent input fields are filled elsewhere 为什么我的程序在填写一个输入时只有 output 一个正确的数字,但如果填写了多个输入,则 output 的结果不正确? - Why does my program only output a correct number when one input is filled out, but output incorrect results if anymore inputs are filled out?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM