![](/img/trans.png)
[英]Jquery How to trigger function on without specifying events in the list
[英]How to force jquery's trigger function to trigger all events in forloop?
我正在使用js,php,jquery將復選框輸入字段的值保存在javascript數組中的頁面上工作,后來又使用了該數組,但是我不知道為什么當我從php foreach循環中手動刪除字段時為每個復選框賦予不同的值, 一切正常,正如您在jsfiddle中可以正常運行所看到的那樣 ,它首先顯示hi ,而不是hi,bi ,最后顯示hi,bi,byby按預期。
但是,當我使用php的foreach循環填充這些輸入字段的值時,它第一個顯示空白,而不是hi而不是hi,bi ,並且不顯示最后一個hi,bi,goodby 。 我不知道我在哪里得到這個錯誤。
我的html代碼;
<input type="checkbox" id="selectall"/>Select all<br>
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="hi" onclick="click_alert(this.value)"/>Check box 1<br>
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="bi" onclick="click_alert(this.value)"/>Check box 2<br>
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="goodby" onclick="click_alert(this.value)"/>Check box 3<br>
以下是我輸入字段的php代碼。
<?php
foreach ($result as $value)
{
?>
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="<?= $value['p_id'] ?>" onclick="showPause('show_hide_multi_pause')"/>
<?php
}
?>
以下是結果數組
Array
(
[0] => Array
(
[p_id] => 110
[left(p_title,30)] => Samsung GALAXY Note II N7100 [
[left(p_features,30)] => OS : Android , Display : 4.8&q
[p_status] => 1
[od_price] => 800
[od_offer_price] => 750
[pi_image1] => Samsung-Galaxy-S3-Neo_1_110.jpg
[o_sqty] => 65
[o_cond] => New
[od_htime] => 2 Business days
)
[1] => Array
(
[p_id] => 127
[left(p_title,30)] => Samsung Galaxy Note Edge SM-N9
[left(p_features,30)] => this is DeMo FeAtUrE
[p_status] => 1
[od_price] => 654
[od_offer_price] =>
[pi_image1] => Samsung Galaxy Note Edge SM-N915G_1_127.jpg
[o_sqty] => 65
[o_cond] => Old
[od_htime] =>
)
)
並且我將輸入值插入為$value['p_id']
。
我們不能對同一頁面中的多個元素使用相同的ID 。 您可以為每個復選框使用類 ,而不是使用Id 。 例如,代替$('input#live_prod_input').each(function() {})
使用$('input.live_prod_input').each(function(){})
在以下復選框中
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="hi" onclick="click_alert(this.value)"/>Check box 1<br>
將上述ID替換為class。
我已經在服務器上重現了您的代碼,並且只需稍作更改即可工作:
在php
循環上嘗試更改
onclick="showPause('show_hide_multi_pause')"
至
onclick="click_alert(this.value)"
這是對我有用的php循環:
foreach ($result as $value)
{
$p_id = $value['p_id'];
echo <<< LOB
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="$p_id" onclick="click_alert(this.value)"/>
LOB;
}
完整的HTML代碼:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
window.click_alert = function(val) //equivalent to function click_alert()
{
csv_pause = [];
var chboxs = document.getElementsByName("live_prod_chk");
for (var i = 0; i < chboxs.length; i++) {
if (chboxs[i].checked) {
csv_pause.push(chboxs[i].value);
}
}
alert(csv_pause);
}
$(document).ready(function() {
$('#selectall').click(function() { //on click
if(this.checked) { // check select status
$('input#live_prod_input').each(function() { //loop through each checkbox
// this.checked = true; //select all checkboxes with class "checkbox1"
$(this).trigger('click');
// $('input#live_prod_input').trigger('click');
}
);
}else{
$('input#live_prod_input').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
});
</script>
</head>
<body>
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="110" onclick="click_alert(this.value)"/>
<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="120" onclick="click_alert(this.value)"/>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.