简体   繁体   English

如何强制jQuery的触发函数来触发forloop中的所有事件?

[英]How to force jquery's trigger function to trigger all events in forloop?

I am working on a page where I am using js, php, jquery for saving checkbox input field's value in a javascript array, and later I am using that array, but I dont know why when I am removing fields from php foreach loop and manually giving different value to every checkbox everything works fine as you can see in jsfiddle which is working fine, it is first displaying hi , than hi,bi and in last hi,bi,goodby as expected. 我正在使用js,php,jquery将复选框输入字段的值保存在javascript数组中的页面上工作,后来又使用了该数组,但是我不知道为什么当我从php foreach循环中手动删除字段时为每个复选框赋予不同的值, 一切正常,正如您在jsfiddle中可以正常运行所看到的那样 ,它首先显示hi ,而不是hi,bi ,最后显示hi,bi,byby按预期。

But when I am populating these input field's value using foreach loop of php, it is 1st displaying blank, than hi than hi,bi and it is not displaying last hi,bi,goodby . 但是,当我使用php的foreach循环填充这些输入字段的值时,它第一个显示空白,而不是hi而不是hi,bi ,并且不显示最后一个hi,bi,goodby I dont know where I am getting this error. 我不知道我在哪里得到这个错误。

my html code; 我的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>

following is my php code for input field. 以下是我输入字段的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
 }
?>

following is result array 以下是结果数组

    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] => 
    )
)

and I am inserting input value as $value['p_id'] . 并且我将输入值插入为$value['p_id']

We can not use same Id for multiple elements in same page. 我们不能对同一页面中的多个元素使用相同的ID Instead of using Id you can use class for each checkbox. 您可以为每个复选框使用 ,而不是使用Id For example, Instead of $('input#live_prod_input').each(function() {}) 例如,代替$('input#live_prod_input').each(function() {})

use $('input.live_prod_input').each(function(){}) 使用$('input.live_prod_input').each(function(){})

in following checkbox 在以下复选框中

<input id="live_prod_input" type="checkbox" name="live_prod_chk" value="hi" onclick="click_alert(this.value)"/>Check box 1<br>

Replace above Id with class. 将上述ID替换为class。

I've reproduced your code on my server and it works with a small change: 我已经在服务器上重现了您的代码,并且只需稍作更改即可工作:

On the php loop try changing php循环上尝试更改

onclick="showPause('show_hide_multi_pause')"

to

onclick="click_alert(this.value)"

This is the php loop that works for me: 这是对我有用的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;
}

COMPLETE HTML CODE: 完整的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM