在调用jquery函数之后,如何从Mysql数据库中的布尔值中选择一个复选框?这里有什么,但我不确定如何选中复选框以及将其放在jquery中的位置。 (FreeShip& BizlicSent)是需要进入复选框的布尔值。任何帮助将不胜感激。
$(function() {
$('#Customer_ID').val("");
$('#Name').val("");
$('#BillingEmail').val("");
$('#FreeShip').val("");
$('#BizlicSent').val("");
$("#searchterm").autocomplete({
source: "Customer.QUERY.php",
minLength: 3,
autoFocus: true,
select: function(event, ui) {
$('#Customer_ID').val(ui.item.Customer_ID);
$('#FirstName').val(ui.item.FirstName);
$('#BillingEmail').val(ui.item.BillingEmail);
$('#FreeShip').val(ui.item.FreeShip);
$('#BizlicSent').val(ui.item.BizlicSent);
}
});
});
这是包含javascript / jquery信息的页面
<?php include '../JQ/jquery.php';?>
<html>
<head>
<script type="text/javascript">
$(function() {
$('#Customer_ID').val("");
$('#Name').val("");
$('#BillingEmail').val("");
$('#FreeShip').val("");
$('#BizlicSent').val("");
$("#searchterm").autocomplete({
source: "Customer.QUERY.php",
minLength: 3,
autoFocus: true,
select: function(event, ui) {
$('#Customer_ID').val(ui.item.Customer_ID);
$('#FirstName').val(ui.item.FirstName);
$('#BillingEmail').val(ui.item.BillingEmail);
$('#FreeShip').attr("checked", ui.item.FreeShip);
$('#BizlicSent').attr("checked", ui.item.BizlicSent);
}
});
});
</script>
</head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="post">SEARCH:
<input type="text" id="searchterm" size="60" onClick="this.form.reset()"/>
</form>
<form method="POST" name="form" id="my_form">
<input type="text" name="Customer_ID" id="Customer_ID">
Customer ID<br>
<input type="text" name="FirstName" id="FirstName">
First Name<br>
<input type="text" name="BillingEmail" id="BillingEmail">
Billing Email<br>
FreeShip<input type="checkbox" name="FreeShip" id="FreeShip"><br>
BizlicSent<input type="checkbox" name="BizlicSent" id="BizlicSent"><br>
<br>
<input type="submit" name="button" value="UPDATE">
</form>
</body>
</html>
这是QUERY页面。
<?php include_once '../connection.php';?>
<?php
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
$return_arr = array();
if ($conn)
{
$ac_term = "%".$_GET['term']."%";
$query = "SELECT
Customer_ID, FirstName, BillingEmail, FreeShip, BizlicSent,
CONCAT_WS(' ', Customer_ID, FirstName, BillingEmail) AS displayName
FROM CustomerListNew
WHERE `Customer_ID` LIKE :term
OR `FirstName` LIKE :term
OR `BillingEmail` LIKE :term
";
$result = $conn->prepare($query);
$result->bindValue(":term",$ac_term);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$row_array['value'] = $row['displayName'];
$row_array['Customer_ID'] = $row['Customer_ID'];
$row_array['FirstName'] = $row['FirstName'];
$row_array['BillingEmail'] = $row['BillingEmail'];
$row_array['FreeShip'] = $row['FreeShip'];
$row_array['BizlicSent'] = $row['BizlicSent'];
array_push($return_arr,$row_array);
}
}
$conn = null;
echo json_encode($return_arr);
?>
这是JSON的回复。
[{"value":"asdf asdf asdf","Customer_ID":"asdf","FirstName":"asdf","BillingEmail":"asdf","FreeShip":"1","BizlicSent":"0"},{"value":"qwer qwer qwer","Customer_ID":"qwer","FirstName":"qwer","BillingEmail":"qwer","FreeShip":"0","BizlicSent":"1"},{"value":"aaaa aaaa aaaa","Customer_ID":"aaaa","FirstName":"aaaa","BillingEmail":"aaaa","FreeShip":"1","BizlicSent":"0"},{"value":"bbbb bbbb bbbb","Customer_ID":"bbbb","FirstName":"bbbb","BillingEmail":"bbbb","FreeShip":"0","BizlicSent":"1"},{"value":"cccc cccc cccc","Customer_ID":"cccc","FirstName":"cccc","BillingEmail":"cccc","FreeShip":"1","BizlicSent":"1"},{"value":"dddd dddd dddd","Customer_ID":"dddd","FirstName":"dddd","BillingEmail":"dddd","FreeShip":"0","BizlicSent":"0"}]
答案 0 :(得分:0)
好!这是现在正在运作的解决方案......
select: function(event, ui) {
$('#Customer_ID').val(ui.item.Customer_ID);
$('#FirstName').val(ui.item.FirstName);
$('#BillingEmail').val(ui.item.BillingEmail);
if (ui.item.FreeShip == "1") {
$('#FreeShip').prop('checked', true);
} else {
$('#FreeShip').prop('checked', false);
}
if (ui.item.BizlicSent == "1") {
$('#BizlicSent').prop('checked', true);
} else {
$('#BizlicSent').prop('checked', false);
}
答案 1 :(得分:0)
缺少=
符号。
您必须使用:
$('#FreeShip').prop('checked', ui.item.FreeShip === "1");