JQuery 自动完成返回选中的复选框

发布于 2024-12-11 19:55:40 字数 4532 浏览 0 评论 0原文

调用 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>

这是查询页面。

<?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"}]

After calling the jquery function how do you make a checkbox selected from a Boolean value in a Mysql database? Here's what is have, but I'm not sure how to get the check box selected and where to put it in the jquery. (FreeShip & BizlicSent) are the Boolean values that need to go into the check boxes. Any help would be greatly appreciated.

$(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);
                }
            });
     }); 

New Edit with more data on the pages

This is the page that have the javascript/jquery info

<?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>

This is the QUERY page.

<?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);
?>

Here's the JSON response.

[{"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"}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城 2024-12-18 19:55:40

好的!这是现在有效的解决方案......

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);
    }

Okay! here's the solution that's working now....

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);
    }
童话里做英雄 2024-12-18 19:55:40

缺少符号 =

您必须使用:

$('#FreeShip').prop('checked', ui.item.FreeShip === "1");

The sign = is missing.

You have to use:

$('#FreeShip').prop('checked', ui.item.FreeShip === "1");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文