我正在四处转转。我是JQUERY的新手,到目前为止它真棒!我已经在互联网上看了很多东西,在这里回答了问题,但我还没有找到正确的答案。现在我正在自杀,试图弄清楚我做错了什么。 .autocomplete工作正常,基本的东西就像从MYSQL数据库中返回一个字符串并将其输入到我想要的形式。但我遇到的问题是返回一个布尔值并用该值标记一个复选框。我现在得到的是所有方框都被检查,即使值为0。
<?php require_once('../connection.php'); ?>
<?php include '../_includes/jq.inc.php';?>
<html>
<head>
<script type="text/javascript">
$(function() {
$('#Company_Name').val("");
$('#Manager').val("");
$('#Phone').val("");
$('#Contacted').val("");
$('#Pitched').val("");
$('#Interested').val("");
$("#autoSearch").autocomplete({
source: "Test.QUERY.php",
minLength: 2,
select: function(event, ui) {
$('#Company_Name').val(ui.item.Company_Name);
$('#Manager').val(ui.item.Manager);
$('#Phone').val(ui.item.Phone);
$('#Contacted').attr("checked", true);
$('#Pitched').attr("checked", true);
$('#Interested').attr("checked", true);
}
});
});
</script>
</head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="post">SEARCH:
<input type="text" id="autoSearch" size="60" onClick="this.form.reset()"/>
</form>
<form method="POST" name="form">
<input type="text" name="Company_Name" id="Company_Name">Company Name<br>
<input type="text" name="Manager" id="Manager">Manager<br>
<input type="text" name="Phone" id="Phone">Phone<br>
Contacted<input type="checkbox" name="Contacted" id="Contacted"><br>
Pitched<input type="checkbox" name="Pitched" id="Pitched"><br>
Interested<input type="checkbox" name="Interested" id="Interested">
<br>
<input type="submit" name="button" value="UPDATE">
</form>
</body>
</html>
Company_Name varchar(45)
Manager varchar(45)
Phone varchar(45)
Contacted tinyint(1)
Pitched tinyint(1)
Interested tinyint(1)
<?php include_once '../conn.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
Company_Name, Manager, Phone, Contacted, Pitched, Interested,
CONCAT_WS(' ', Company_Name, Manager, Phone) AS autoNameShow
FROM CustomerList
WHERE `Company_Name` LIKE :term
OR `Manager` LIKE :term
OR `Phone` LIKE :term
";
$result = $conn->prepare($query);
$result->bindValue(":term",$ac_term);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$row_array['value'] = $row['autoNameShow'];
$row_array['Company_Name'] = $row['Company_Name'];
$row_array['Manager'] = $row['Manager'];
$row_array['Phone'] = $row['Phone'];
$row_array['Contacted'] = $row['Contacted'];
$row_array['Pitched'] = $row['Pitched'];
$row_array['Interested'] = $row['Interested'];
array_push($return_arr,$row_array);
}
}
$conn = null;
echo json_encode($return_arr);
?>
所以,百万美元的问题是......我做错了什么?我该如何解决?
<?php include_once '../conn.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
Company_Name, Manager, Phone, Contacted, Pitched, Interested,
CONCAT_WS(' ', Company_Name, Manager, Phone) AS autoNameShow
FROM CustomerList
WHERE `Company_Name` LIKE :term
OR `Manager` LIKE :term
OR `Phone` LIKE :term
";
$result = $conn->prepare($query);
$result->bindValue(":term",$ac_term);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$return_arr[] = array(
'value' => $row['autoNameShow'],
'Company_Name' => $row['Company_Name'],
'Manager' => $row['Manager'],
'Phone' => $row['Phone'],
'Contacted' => $row['Contacted'],
'Pitched' => $row['Pitched'],
'Interested' => $row['Interested'],
);
}}
$conn = null;
echo json_encode($return_arr);
?>
<?php include '../_includes/jq.inc.php';?>
<html>
<head>
<script type="text/javascript">
var $form = $('#my_form');
$(function() {
$('#Company_Name').val("");
$('#Manager').val("");
$('#Phone').val("");
$('#Contacted').val("");
$('#Pitched').val("");
$('#Interested').val("");
$('#autoSearch').autocomplete({
source: 'Test.QUERY.php',
minLength: 2,
select: function(event, ui) {
$form.find('input').val('');
$form.find('input[type="checkbox"]').attr('checked', '');
$('#Company_Name').val(ui.item.Company_Name);
$('#Manager').val(ui.item.Manager);
$('#Phone').val(ui.item.Phone);
if (ui.item.Contacted) { $('#Contacted').attr('checked', 'checked'); }
if (ui.item.Pitched) { $('#Pitched').attr('checked', 'checked'); }
if (ui.item.Interested) { $('#Interested').attr('checked', 'checked'); }
}
});
});
</script>
</head>
<body>
<form action="<?php echo $PHP_SELF;?>" method="post">SEARCH:
<input type="text" id="autoSearch" size="60" onClick="this.form.reset()"/>
</form>
<form method="POST" name="form" id="my_form">
<input type="text" name="Company_Name" id="Company_Name">Company Name<br>
<input type="text" name="Manager" id="Manager">Manager<br>
<input type="text" name="Phone" id="Phone">Phone<br>
Contacted<input type="checkbox" name="Contacted" id="Contacted"><br>
Pitched<input type="checkbox" name="Pitched" id="Pitched"><br>
Interested<input type="checkbox" name="Interested" id="Interested">
<br>
<input type="submit" name="button" value="UPDATE">
</form>
</body>
</html>
我做错了什么?
[{"value":"Company Test 1 Manager Test 1 111-111-1111","Company_Name":"Company Test 1","Manager":"Manager Test 1","Phone":"111-111-1111","Contacted":"0","Pitched":"0","Interested":"0"},{"value":"Company Test 2 Manager Test 2 222-222-2222","Company_Name":"Company Test 2","Manager":"Manager Test 2","Phone":"222-222-2222","Contacted":"0","Pitched":"1","Interested":"1"},{"value":"Company Test 3 Manager Test 3 333-333-3333","Company_Name":"Company Test 3","Manager":"Manager Test 3","Phone":"333-333-3333","Contacted":"0","Pitched":"0","Interested":"1"},{"value":"Company Test 4 Manager Test 4 444-444-4444","Company_Name":"Company Test 4","Manager":"Manager Test 4","Phone":"444-444-4444","Contacted":"1","Pitched":"0","Interested":"1"},{"value":"Company Test 5 Manager Test 5 555-555-5555","Company_Name":"Company Test 5","Manager":"Manager Test 5","Phone":"555-555-5555","Contacted":"0","Pitched":"1","Interested":"1"},{"value":"Company Test 6 Manager Test 6 666-666-6666","Company_Name":"Company Test 6","Manager":"Manager Test 6","Phone":"666-666-6666","Contacted":"1","Pitched":"0","Interested":"1"},{"value":"Company Test 7 Manager Test 7 777-777-7777","Company_Name":"Company Test 7","Manager":"Manager Test 7","Phone":"777-777-7777","Contacted":"0","Pitched":"1","Interested":"1"},{"value":"Company Test 8 Manager Test 8 888-888-8888","Company_Name":"Company Test 8","Manager":"Manager Test 8","Phone":"888-888-8888","Contacted":"1","Pitched":"0","Interested":"1"},{"value":"Company Test 9 Manager Test 9 999-999-9999","Company_Name":"Company Test 9","Manager":"Manager Test 9","Phone":"999-999-9999","Contacted":"0","Pitched":"1","Interested":"0"},{"value":"Company Test 10 Manager Test 10 000-000-0000","Company_Name":"Company Test 10","Manager":"Manager Test 10","Phone":"000-000-0000","Contacted":"1","Pitched":"0","Interested":"1"}]
答案 0 :(得分:1)
当您从自动填充列表中选择一个值时,无论返回的值是什么,您都会自动检查所有三个复选框。
你会想要使用类似的东西:
var $form = $('#my_form');
$('#autoSearch').autocomplete({
source: 'Test.QUERY.php',
minLength: 2,
select: function(event, ui) {
$form.find('input').val('');
$form.find('input[type="checkbox"]').attr('checked', '');
$('#Company_Name').val(ui.item.Company_Name);
$('#Manager').val(ui.item.Manager);
$('#Phone').val(ui.item.Phone);
if (ui.item.Contacted == "1") {
$('#Contacted').attr('checked', 'checked');
}
if (ui.item.Pitched == "1") {
$('#Pitched').attr('checked', 'checked');
}
if (ui.item.Interested == "1") {
$('#Interested').attr('checked', 'checked');
}
}
});
您的表单需要ID:
<form method="POST" name="form" id="my_form">
<input type="text" name="Company_Name" id="Company_Name"> Company Name<br />
...
</form>
关于PHP代码的一个小注释,您可以使用以下代码来协助$ return_arr数组,而不是使用array_push。
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$return_arr[] = array(
'value' => $row['autoNameShow'],
'Company_Name' => $row['Company_Name'],
'Manager' => $row['Manager'],
'Phone' => $row['Phone'],
// ... etc
);
}
答案 1 :(得分:1)
select: function(event, ui) {
$('#Company_Name').val(ui.item.Company_Name);
$('#Manager').val(ui.item.Manager);
$('#Phone').val(ui.item.Phone);
if (ui.item.Contacted == "1") {
$('#Contacted').prop('checked', true);
} else {
$('#Contacted').prop('checked', false);
}
if (ui.item.Pitched == "1") {
$('#Pitched').prop('checked', true);
} else {
$('#Pitched').prop('checked', false);
}
if (ui.item.Interested == "1") {
$('#Interested').prop('checked', true);
} else {
$('#Interested').prop('checked', false);
}
}
答案 2 :(得分:0)
如果您使用的是jquery 1.6或更高版本,则需要使用.prop
代替.attr
复选框。如果您必须使用.attr
,则正确的语法为.attr('checked', 'checked')
答案 3 :(得分:0)
您使用以下代码明确检查所有复选框:
$('#Contacted').attr("checked", true);
$('#Pitched').attr("checked", true);
$('#Interested').attr("checked", true);
实际上,你现在所做的就是检查所有方框,这是正常的。
答案 4 :(得分:0)
修改自动填充功能,挑选复选框项目,而不是将其设置为选中:
$(function() {
$('#Company_Name').val("");
$('#Manager').val("");
$('#Phone').val("");
$('#Contacted')[0].checked = false;
$('#Pitched')[0].checked = false;
$('#Interested')[0].checked = false;
$("#autoSearch").autocomplete({
source: "Test.QUERY.php",
minLength: 2,
select: function(event, ui) {
$('#Company_Name').val(ui.item.Company_Name);
$('#Manager').val(ui.item.Manager);
$('#Phone').val(ui.item.Phone);
$('#Contacted')[0].checked = ui.item.Contacted;
$('#Pitched')[0].checked = ui.item.Pitched;
$('#Interested')[0].checked = ui.item.Interested;
}
});
});
我的假设是ui.item.Contacted等包含true / false值。
您可以在选择中添加console.log(ui.item.Company_name+ ":"+ui.item.Contacted);
来测试这些值,或者只是alert(ui.item.Company_name+ ":"+ui.item.Contacted);
答案 5 :(得分:0)
这是the solution you came up with的更紧凑版本。
select: function(event, ui) {
$('#Company_Name').val(ui.item.Company_Name);
$('#Manager').val(ui.item.Manager);
$('#Phone').val(ui.item.Phone);
$('#Contacted').prop('checked', ui.item.Contacted == '1');
$('#Pitched').prop('checked', ui.item.Pitched == '1');
$('#Interested').prop('checked', ui.item.Interested == '1');
}