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

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。希望這就是您想要實現的目標。
另外,如果您想知道從數據庫中獲取了多少行,請count()
在 php.ini 中使用。
$form_count = count($form);
- 3 回答
- 0 關注
- 163 瀏覽
添加回答
舉報