在循环中选择动态元素 id

我是 js 和 jquery 的新手,需要一些帮助。我有通过 php 从数据库返回的数据,例如:


foreach ($form as $value) {

  $input = '<input type="number" id="tbpersentase'.$i.'" min="0" max="100" value="'.$value->persentase.'" title="Progres">';

  $inputdnone = '<input type="number" id="persentase'.$i.'" min="0" max="100" value="'.$value->persentase.'">'; //this input should not appear in view

  $i++;

}

 $row = '<input type="number" id="formJum" value="'.$i.'">';


我想要的 html 结果可能是这样的:


<input id="tbpersentase0" value="myVal">

<input id="persentase0" value="myVal">

<input id="tbpersentase1" value="myVals">

<input id="persentase1" value="myVals">

...

// and so on as many as the data retrieve from db

<input id="formJum" value="rowCount">


在我的项目中,需要在id='"tbpersentase"$i'用户更改输入值时,然后输入id='"persentase"$i'值更改为任何id='"tbpersentase"$i'值。我使用这样的一些代码:


var formJum = $('#formJum').val();

for(i=0; i<formJum; i++){

  $('#tbpersentase'+i).change(function(){

    var tbpersentase = $(this).val();

    $('#persentase'+i).val(tbpersentase);

  })

}

浏览器没有给我任何错误,所以我认为我的代码已经完成。但是当我用相同id='"tbpersentase"$i'的元素id='"persentase"$i'值更改输入值时,i它不会改变。


我的整个元素代码如下所示:


<div class="col-sm-7 px-0 reportsForApps d-none">

  <div class="px-3">

    <table class="table dttables" id="dtForm"> // data-tables client side processing

      <thead class="d-none">

        <tr>

          <th class="d-none">-</th>

          <th class="d-none">-</th>

        </tr>

      </thead>

      <tbody id="inputform">

        // #tbpersentase goes here for user input ..

      </tbody>

    </table>

    <div id=""> // this doesnt appear to user page

    <input type="number" id="formJum" value="">

    </div>

    <div id="inputProgres"> // this doesnt appear to user page

      // #persentase goes here ..

    </div>

  </div>

</div>

ajax设置的所有值和元素。知道我与我的代码有什么关系吗?谢谢你


GCT1015
浏览 91回答 3
3回答

jeck猫

您可以使用class作为选择器,因为这对于多个元素可以是相同的。使用 jQuerydata存储索引。$i=0;&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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach ($form as $value) {&nbsp; &nbsp; $input = '<input type="number" class="percentage" data-index="'.$i.'" value="'.$value->persentase.'" min="0" max="100" title="Progres">';&nbsp; &nbsp; $inputdnone = '<input type="number" id="persentase'.$i.'" min="0" max="100" value="'.$value->persentase.'">'; //this input should not appear in view&nbsp; &nbsp; $i++;&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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}在 jQuery 部分,不需要使用循环,只需为percentage类编写更改函数。只要输入的值发生更改,就会触发此操作:$(".reportsForApps").on("change", ".percentage", function(){ // 'parentElementId' should be replaced with actual parent element id.&nbsp; &nbsp; var tbpersentase = $(this).val();&nbsp;&nbsp; &nbsp; var index = $(this).data("index");&nbsp; &nbsp; $('#persentase'+index).val(tbpersentase);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});

慕村225694

添加数据类型 attrdata-group="tbpersentase"并在您的函数中调用它$input = null;$inputdnone = null;foreach ($form as $key => $value) { // you could also use $key value for increment&nbsp; &nbsp; $input .= '<input type="number" data-group="tbpersentase" id="tbpersentase'.$key.'" min="0" max="100" value="'.$value.'" title="Progres">';&nbsp; &nbsp; $inputdnone .= '<input type="hidden" id="persentase'.$key.'" min="0" max="100" value="'.$value.'">'; //this input should not appear in view}&nbsp;// place in html to echo results of dynamically created inputs from DB info&nbsp;<?=$input?>&nbsp;<?=$inputdnone?>// JQuery 3.4.1$( "input[data-group='tbpersentase']" ).change(function() {&nbsp; var $this = $(this).val(); // get users value of the changing input field&nbsp; var tbpersentaseID = $(this).attr('id'); // get the changing input fields ID so we can remove IDs alpha chars&nbsp; var getID = tbpersentaseID.replace(/[^0-9]/g,''); // declare a new variable containing the numbers for the selected ID&nbsp; var persentaseID = 'persentase' + getID; // Concatenate desired alpha ID name to selected key value&nbsp;&nbsp; $("#"+persentaseID).val($this); // set value});在 chrome 中测试并将 的值更改为#persentase输入的值,#tbpersentase但保留原始 ID 在#persentase. 希望这是您想要实现的目标。此外,如果您想知道从 DB 中获得了多少行,请count()在 php.ini 中使用。$form_count&nbsp;=&nbsp;count($form);

胡子哥哥

您必须使用 change() 函数对每个 #formJum 值更改执行该代码。如果您在每次输入更改中正确处理这些事件处理程序,也会更好。
打开App,查看更多内容
随时随地看视频慕课网APP