复选框 - 选中取消选中功能无效

时间:2016-02-15 17:26:35

标签: javascript php jquery checkbox

我的情况:

我正在开发购物车应用程序,它包含一些过滤器:

  1. 按颜色过滤(复选框)

  2. 按样式过滤(复选框)

  3. 选择一些颜色我的网址就像这样:

    http://example.com/women/try.php?color=10,11,12,13
    

    我的问题:

    1. 取消选中某些颜色时,相关的参数不会从网址中清除。

    2. 当我选择一些样式时,我希望网址如下:

      http://example.com/women/try.php?color=10,11,12,13&style=1,2,3

    3. 请帮助我了解如何实现此功能。

      我的代码:

      <?php
          $colors = $_GET['color'];
          $sel_colors = explode(',', $colors); 
          foreach($sel_colors as $k=>$v) {
              $c['check'][$v] = $v;
          }
      
          for($i=10;$i<=14;$i++) { ?>
              <input type="checkbox" name="color[]" value="<?php echo $i; ?>"  <?php echo $check_value = ($c['check'][$i]) ? 'checked' : '0'; ?> >
              <label>Color #<?php echo $i.'--'.$check_value; ?></label><?php
          }
      ?><br/><br/><br/>
      <input type="checkbox" name="type" value="1" >
      <label>style #1</label>
      <input type="checkbox" name="type" value="2" >
      <label>style #2</label>
      <input type="checkbox" name="type" value="3" >
      <label>style #3</label>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js" ></script>
      <script type="text/javascript">
          var baseUrl = 'http://website/women/try.php?color=';
          $(document).ready(function () {
              // listen to change event (customize selector to your needs)
              $('input[type=checkbox]').change(function (e) {
                  e.preventDefault();
                  if ($(this).is(':checked')) {
                      // read in value
                      var queryString = $(this).val();
                      // loop through siblings (customize selector to your needs)
                      var s = $(this).siblings();
                      $.each(s, function () {
                          // see if checked
                          if ($(this).is(':checked')) {
                              // append value
                              queryString += ',' + $(this).val();
                          }
                      });
                      // jump to url
                      window.location = baseUrl + queryString;
                  }
              });
          });
      </script>
      

2 个答案:

答案 0 :(得分:1)

使用map()函数获取所有选中的颜色和样式checkbox值,如下所示。

var baseUrl = 'http://website/women/try.php?color=';

$('input[name="color[]"], input[name="type"]').change(function () {
    var colors = $('input[name="color[]"]:checked').map(function () { return this.value; }).get().join();
    var styles = $('input[name="type"]:checked').map(function () { return this.value; }).get().join();

    window.location = baseUrl + colors + '&style=' + styles;
});

答案 1 :(得分:1)

以下是工作解决方案的代码段。颜色名称复选框已从color[]更改为color

&#13;
&#13;
var baseUrl = 'http://website/women/try.php?';
    $(document).ready(function () {

      // listen to change event (customize selector to your needs)
      $('input[type=checkbox]').change(function (e) {
        
        //Get all the selected color values
        var queryString = "color="+$('[name="color"]:checked')
                          .map(function() {return this.value;}).get().join(',');
        
        //Append all the selected styles
        queryString += "&style="+$('[name="type"]:checked').map(function() {return this.value;}).get().join(',');
      
        //reload page - commented for this snippet
        //window.location = baseUrl + queryString;
        alert(baseUrl + queryString);
      });
      
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Color filter -->
<input type="checkbox" name="color" value="1"  checked >
    <label>Color 1</label>
<input type="checkbox" name="color" value="2"  checked >
    <label>Color 2</label>
<input type="checkbox" name="color" value="3"  checked >
    <label>Color 3</label>
<input type="checkbox" name="color" value="4"  checked >
    <label>Color 4</label>

<BR><BR>
  
<!-- Style filter -->
<input type="checkbox" name="type" value="1" >
<label>style #1</label>

<input type="checkbox" name="type" value="2" checked>
<label>style #2</label>

<input type="checkbox" name="type" value="3" checked>
<label>style #3</label>
&#13;
&#13;
&#13;