duanpo2813 2019-06-17 12:18
浏览 46

如何使用ajax php过滤复选框

I am using ajax in codeigniter, I have different checkbox now i want whenever someone checked checkboxes,result should come according to user selection of checkboxes,so how can i do this ? here is my code

<input type="checkbox" class="styled" id="Baker" value="Bakers">
<label for="role1">Bakers</label>

<input type="checkbox" class="styled" id="Bakerd" value="Bakersd">
<label for="role1">Bakersd</label>

<script>
    $('.styled').click(function() {
    alert($(this).attr('id'));  
       if(this.checked){
            $.ajax({
                type: "POST",
                url: 'searchOnType.php',
                data: $(this).attr('id'), 
                success: function(data) {
                    alert('it worked');
                    alert(data);
                    $('#container').html(data);
                },
                 error: function() {
                    alert('it broke');
                },
                complete: function() {
                    alert('it completed');
                }
            });

            }
      });
</script>   
  • 写回答

1条回答 默认 最新

  • douji5746 2019-06-17 12:51
    关注

    Html

    <ul class="unstyled centered">
        <li>
            <input type="checkbox" class="styled-checkbox" id="Baker" value="" onchange="checkBoxOnChange(this);">
          <label for="role1">Baker</label>
        </li>
        <li>
          <input type="checkbox" class="styled-checkbox" id="Barista" value="" onchange="checkBoxOnChange(this);">
          <label for="role2">Barista</label>
        </li>
        <li>
          <input type="checkbox" class="styled-checkbox" id="Bartender" value="" onchange="checkBoxOnChange(this);">
          <label for="role3">Bartender</label>
    
        </li>
        <li id="filter">
    
        </li>
      </ul>
    

    jQuery Script

    <script>
            function checkBoxOnChange(isChecked){
                if($(isChecked).is(":checked")){
                    console.log("check");
                    $.ajax({
                    url: 'test.php',
                    type: 'POST',
                    data: {id: $(isChecked).attr('id')}, // I suggest you to use $(isChecked).val(). Add value in html same as id
                    success: function(data){
                        console.log(data);
                        $("#filter").text(data);
                    }
                    }
                  );
                }else{
                    console.log("Uncheck");
                }
            }
            </script>
    

    Php Script

    test.php example just return a selected checkbox value

    <?php 
    
    echo ($_REQUEST['id']);
    exit;
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计