简体   繁体   English

如何通过复选框选中或取消选中过滤数据

[英]How can i filter data by checkbox check or uncheck

I have a few checkbox like below. 我有几个如下的复选框。 and when one checkbox checked it must load result from mysql and when unchecked again it must load result without page refresh. 当选中一个复选框时,它必须加载mysql的结果,而再次取消选中时,它必须加载没有页面刷新的结果。

Almost it is same with http://www.facebook.com/find-friends/browser/ http://www.facebook.com/find-friends/browser/几乎相同

How can i do? 我能怎么做?

 <div class="tags">  <label><input type="checkbox" class="arts" /> Arts </label> <label><input type="checkbox" class="computers" /> Computers </label> <label><input type="checkbox" class="health" /> Health </label> <label><input type="checkbox" class="video-games" /> Video Games </label> </div>

您可以使用JavaScript和jQuery来执行此操作: http : //api.jquery.com/jQuery.get/

This is most easily accomplished using jQuery (as stated above). 使用jQuery(如上所述)最容易做到这一点。 In general, you'll go through a series of 'find something' then 'do something' tasks. 通常,您将经历一系列的“发现某事”然后“做某事”任务。 I'm no javascript expert, but my feeble attempt at the logic should get you started on the right path. 我不是javascript专家,但是我在逻辑方面的微不足道的尝试应该可以帮助您正确地开始工作。

The jQuery logic will look something like this: jQuery逻辑如下所示:

Hope this helps. 希望这可以帮助。

You need to use Ajax. 您需要使用Ajax。

As people suggested, jQuery allows you to do this easily. 正如人们所建议的那样,jQuery使您可以轻松地做到这一点。

Example: 例:

In a JavaScript file: 在JavaScript文件中:

$.ajax({
  url: '/get-data',
  type: 'GET',
  data: { tags: anArrayOfCheckedCheckboxes }, // get the checked checkboxes the way you want
  complete: function(response) {
    // do what do you need with the response (display results for example)
  }
});

On PHP's side ( /get-data URL): 在PHP方面( / get-data URL):

$tags = $_GET['tags'];
// get the results from the database
return json_encode($results);

您可以获取已选中复选框$('[name =” hobbies”:checked]')。val()的值,对于未选中复选框,可以使用$(“ input:checkbox:not(:checked)”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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