I have a jquery question. I currently store some data in a mysql table which is delimited by commas. Here is an example of the data stored
Tbl_stage " concept , prerevenue , 10mm , 20mm "
Once I echo this data in php, I need each check box to be checked if it is contained in the array
<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!
array
without any spaces around the string
. checkbox
elements by testing text
of label with array
of trimmed
values retrieved from DB
. jQuery.prop(PROPERTY, STATE)
to set checked
property. 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
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
You can loop it and set the checked value to 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
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.