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

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

在循環中選擇動態元素 id

在循環中選擇動態元素 id

明月笑刀無情 2023-09-25 15:49:21
我是 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設置的。知道我要對我的代碼做什么嗎?謝謝
查看完整描述

3 回答

?
偶然的你

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

您可以用作class選擇器,因為這對于多個元素來說是相同的。使用 jQuerydata來存儲索引。


$i=0;                                                                           

foreach ($form as $value) {

    $input = '<input type="number" class="percentage" data-index="'.$i.'" value="'.$value->persentase.'" min="0" max="100" title="Progres">';

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

    $i++;                                                                                 

}

在jQuery部分,不需要使用循環,只需為percentage類編寫更改函數。每當輸入值更改時都會觸發此操作:


$(".reportsForApps").on("change", ".percentage", function(){ // 'parentElementId' should be replaced with actual parent element id.

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

    var index = $(this).data("index");

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

});


查看完整回答
反對 回復 2023-09-25
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

您必須使用change() 函數在每次#formJum 值更改時執行該代碼。

如果您在每次輸入更改中正確處理這些事件處理程序,也會更好。


查看完整回答
反對 回復 2023-09-25
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

添加數據類型 attrdata-group="tbpersentase"并在函數中調用它


$input = null;

$inputdnone = null;


foreach ($form as $key => $value) { // you could also use $key value for increment

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

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

}

 // place in html to echo results of dynamically created inputs from DB info

 <?=$input?>

 <?=$inputdnone?>


// JQuery 3.4.1

$( "input[data-group='tbpersentase']" ).change(function() {

  var $this = $(this).val(); // get users value of the changing input field

  var tbpersentaseID = $(this).attr('id'); // get the changing input fields ID so we can remove IDs alpha chars

  var getID = tbpersentaseID.replace(/[^0-9]/g,''); // declare a new variable containing the numbers for the selected ID

  var persentaseID = 'persentase' + getID; // Concatenate desired alpha ID name to selected key value 


  $("#"+persentaseID).val($this); // set value


});

在 chrome 中進行測試,并將 的值更改#persentase為在 中輸入的值#tbpersentase,但保留 中的原始 ID #persentase。希望這就是您想要實現的目標。

https://img1.sycdn.imooc.com//65113bd10001419706510467.jpg

另外,如果您想知道從數據庫中獲取了多少行,請count()在 php.ini 中使用。

$form_count = count($form);


查看完整回答
反對 回復 2023-09-25
  • 3 回答
  • 0 關注
  • 163 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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