简体   繁体   English

字符串数组,然后选择复选框

[英]String to array, then select check boxes

I have a jquery question. 我有一个jquery问题。 I currently store some data in a mysql table which is delimited by commas. 我目前将一些数据存储在以逗号分隔的mysql表中。 Here is an example of the data stored 这是存储数据的示例

Tbl_stage " concept , prerevenue , 10mm , 20mm " Tbl_stage“概念,预收入,10mm,20mm”

Once I echo this data in php, I need each check box to be checked if it is contained in the array 一旦我在php中回显了这些数据,我就需要检查每个复选框是否包含在数组中

<label><input type="checkbox" id="check-item" class="primary1" checked /> concept </label> 
<label><input type="checkbox" id="check-item" class="primary1" checked /> pre-revenue </label> 
<label><input type="checkbox" id="check-item" class="primary1" /> sample blank data </label> 

Your help is greatly appreciated! 非常感谢您的帮助!

  • First of all prepare an array without any spaces around the string . 首先准备一个arraystring周围没有空格。
  • Filter checkbox elements by testing text of label with array of trimmed values retrieved from DB . 通过使用从DB检索的trimmedarray测试标签text来过滤checkbox元素。
  • Use jQuery.prop(PROPERTY, STATE) to set checked property. 使用jQuery.prop(PROPERTY, STATE)设置checked属性。

 var str = " concept , prerevenue , 10mm , 20mm "; var arr = str.split(' , ').map(function(el) { return el.trim(); }); $('.primary1').filter(function() { return (arr.indexOf($(this).closest('label').text().replace('-', '').trim()) !== -1); }).prop('checked', true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <label> <input type="checkbox" id="check-item" class="primary1" />concept</label> <label> <input type="checkbox" id="check-item" class="primary1" />pre-revenue</label> <label> <input type="checkbox" id="check-item" class="primary1" />sample blank data</label> 

Get the data from table using query and push each element into an array. 使用查询从表中获取数据并将每个元素推入数组。 Then use the array to check each data and echo checked in your checkbox tag 然后使用数组检查每个数据并在您的复选框标记中回显检查

<?php 
  $query = 'Your query here to get the data from database table';
  $results = array();
  while($row = mysql_fetch_assoc($query))
  {
     $results[] = $row;
  }
?>
<label>
  <input type="checkbox" id="check-item" class="primary1" <?php if(in_array("concept",$results)) echo "checked";?> /> concept </label> 
<label>
  <input type="checkbox" id="check-item" class="primary1" <?php if(in_array("pre-revenue",$results)) echo "checked";?> /> pre-revenue </label> 
<label>
  <input type="checkbox" id="check-item" class="primary1" <?php if(in_array("sample blank data",$results)) echo "checked";?> /> sample blank data </label>

We have split function in js ref: http://www.w3schools.com/jsref/jsref_split.asp 我们在js参考中具有split函数: http : //www.w3schools.com/jsref/jsref_split.asp

Split the string that you get from the server.It will be array of words for example 拆分从服务器获取的字符串,例如将是单词数组

var str = "concept , prerevenue , 10mm , 20mm";
var res = str.split(",");

Then you will get array with values concept prerevenue 10mm 20mm 然后您将获得带有值概念的数组,收入为10mm 20mm

You can loop it and set the checked value to true 您可以将其循环并将检查值设置为true

$("input[type=checkbox][value=res[i]").attr("checked","true");

and don't forget to put values of input values same as the label values 并且不要忘记将输入值的值与标签值相同

ref: http://jsfiddle.net/LCU4C/2/ 参考: http : //jsfiddle.net/LCU4C/2/

Update your script as follows: 如下更新脚本:

<?php
$arr = array("concept" , "prerevenue" , "10mm" , "20mm"); ?>
<label><input type="checkbox" id="check-item" class="primary1" <?php echo in_array("concept",$arr) ? "checked" : "" ?> /> concept </label> 
<label><input type="checkbox" id="check-item" class="primary1" <?php echo in_array("prerevenue",$arr) ? "checked" : "" ?> /> prerevenue </label> 

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

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