Home > Backend Development > PHP Tutorial > Traversing 2D array in jQuery - Stack Overflow

Traversing 2D array in jQuery - Stack Overflow

WBOY
Release: 2016-08-04 09:20:22
Original
1348 people have browsed it

html:

<code><ul class="box2" sss="a">
    <div>
        <input class="input checkboxs" aa="001" type="checkbox"><span> 一</span>
    </div>
    <div>
        <input class="input checkboxs" aa="002" type="checkbox"><span> 二</span>
    </div>
</ul>
<ul class="box2" sss="b">
    <div>
        <input class="input checkboxs" aa="001" type="checkbox"><span> 三</span>
    </div>
    <div>
        <input class="input checkboxs" aa="002" type="checkbox"><span> 四</span>
    </div>
</ul></code>
Copy after login
Copy after login

sss represents the area, and a session is generated when the checkbox is clicked, like this: $_SESSION [$sss] [$aa].
The requirement is: when the page is refreshed, the checked items are still checked.
I know that I can set an ID for the checkbox, and it will be checked by default based on the ID setting. It also worked, but mainly I wanted to know how many he checked in area a or b. So I use a 2D array.

The question now is how to find this checkbox in jQuery?

This is the controller under the jQuery and CI framework that I use the id setting to be checked by default.

jQuery:

<code>var checks=$('input:checkbox');
for(var i=0;i<checks.length;i++){
    //将checkbox的id传过去比较,返回存在的id
    $.post('is_session',{checkid:checks[i].id},function(data){
        //存在则默认被选中
        //alert(data);
        $('#'+data).attr('checked',true);
    });
}</code>
Copy after login
Copy after login

CI:

<code>public function is_session(){
    //当点击选项时 将所属id传过来
    $checked=$this->input->post('checkid');
    //$area=$this->input->post('area');
    //判断$_SESSION[$checked],若存在返回$checked(id)
    if(!empty($_SESSION[$checked])){
        echo $checked;
    }
}
</code>
Copy after login
Copy after login

Thank you for your help.

Reply content:

html:

<code><ul class="box2" sss="a">
    <div>
        <input class="input checkboxs" aa="001" type="checkbox"><span> 一</span>
    </div>
    <div>
        <input class="input checkboxs" aa="002" type="checkbox"><span> 二</span>
    </div>
</ul>
<ul class="box2" sss="b">
    <div>
        <input class="input checkboxs" aa="001" type="checkbox"><span> 三</span>
    </div>
    <div>
        <input class="input checkboxs" aa="002" type="checkbox"><span> 四</span>
    </div>
</ul></code>
Copy after login
Copy after login

sss represents the area, and a session is generated when the checkbox is clicked, like this: $_SESSION [$sss] [$aa].
The requirement is: when the page is refreshed, the checked items are still checked.
I know that I can set an ID for the checkbox, and it will be checked by default based on the ID setting. It also worked, but mainly I wanted to know how many he checked in area a or b. So I use 2D array.

The question now is how to find this checkbox in jQuery?

This is the controller under the jQuery and CI framework that I use the id setting to be checked by default.

jQuery:

<code>var checks=$('input:checkbox');
for(var i=0;i<checks.length;i++){
    //将checkbox的id传过去比较,返回存在的id
    $.post('is_session',{checkid:checks[i].id},function(data){
        //存在则默认被选中
        //alert(data);
        $('#'+data).attr('checked',true);
    });
}</code>
Copy after login
Copy after login

CI:

<code>public function is_session(){
    //当点击选项时 将所属id传过来
    $checked=$this->input->post('checkid');
    //$area=$this->input->post('area');
    //判断$_SESSION[$checked],若存在返回$checked(id)
    if(!empty($_SESSION[$checked])){
        echo $checked;
    }
}
</code>
Copy after login
Copy after login

Thank you for your help.

I feel like your idea is wrong
I recommend you a method
$(obj).load("url #local refresh id");
Use jquery for partial refresh, and attach the corresponding attributes during the loop

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template