dongxiangchan0743 2016-02-26 14:03
浏览 55

单击复选框时如何在jquery中创建数组?

I have a checkbox for brands and a check box for prices. Now if a user clicks on the check box I want an array of brand_ids and an array of prices.

<div class="left-filters__inner--block">
  <ul class="filter-data filter-data-brands" id="brands_list">
    @foreach(App\Brand::all() as $brand)
      <li>
        <label for="{{$brand->name}}" class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect">
           <input type="checkbox" name="brands[]" id="{{$brand->name}}" class="mdl-checkbox__input"  data-value="{{$brand->id}}">
           <span class="mdl-checkbox__label">{{$brand->name}}</span>                       
        </label>
      </li>
    @endforeach
  </ul>
</div>

price view with checkbox

<div class="left-filters__inner--block">
    <ul class="filter-data filter-data-price" id="price_list">
        <li>
            <label for="less-20" class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect">
                <input type="checkbox" id="less-20" class="mdl-checkbox__input" name="price" data-value="0,20">
                <span class="mdl-checkbox__label">Less than 20</span>
            </label>
        </li>
        <li>
            <label for="21-50" class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect">
               <input type="checkbox" id="21-50" class="mdl-checkbox__input" name="price" data-value="21,50">
               <span class="mdl-checkbox__label">21  -  50</span>
             </label>
         </li>
         <li>
              <label for="51-100" class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect">
                   <input type="checkbox" id="51-100" class="mdl-checkbox__input"  name="price" data-value="51,100">
                   <span class="mdl-checkbox__label">51  -  100</span>
               </label>
         </li>

Now when user clicks on the checkbox a particular brand or price I want an array which looks like this.

Array
    (
        [brand_id] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

        [price] => Array
        (
            [0] => 0,1000
            [1] => 1000,2000
        )

    )

I want to achieve this using jquery. Please assists

  • 写回答

1条回答 默认 最新

  • dongpang9573 2016-02-26 14:39
    关注

    https://jsfiddle.net/2moqx8da/

    $("input:checkbox").click(function() {
        var brandids=[], prices=[];
        $("input[name='brands']:checked").each(function() {
        brandids.push($(this).val())
      });
      $("input[name='price']:checked").each(function() {
        prices.push($(this).val())
      });
      $('#output').text(JSON.stringify([brandids, prices]));
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算