简体   繁体   English

如何使用jquery根据在第一个文本框中输入的值填充第二个文本框?

[英]How do I populate second textbox based on the value entered in first textbox using jquery?

I am trying to populate the text fields based on the value entered in the first textbox field from the database but I am not getting any response when I enter some value into the first textbox field. 我试图根据在数据库中第一个文本框字段中输入的值填充文本字段,但是当我在第一个文本框字段中输入一些值时我没有得到任何响应。 Please check the code 请检查代码

Javascript: Javascript:

 <script type="text/javascript">
    $("#tin").blur(function () {
    $.post("tin_handle.php",
           {
               tin: $(this).val() 
        }, 
    function (data){
        $("#cname").val(data.cname);
        $("#caddress").val(data.caddress);
    });

</script>

Tin_handle.php: Tin_handle.php:

<?php
    $tn = trim($_POST['tin']);

    require_once("sqlconnect.php");

    $q="SELECT CONCAT(address,',',city,',',state) AS caddress,cname,tin FROM company WHERE tin=$tn;                                                                          

    $r = @mysqli_query ($dbc, $q);

    //$arr=array();
    while ($row = mysql_fetch_array($r))
    {

        $arr=array('cname'=>$cname, 'caddress'=>$caddress);
        echo json_encode($arr);

    }

?>

Have you checked a JavaScript error console? 您是否检查过JavaScript错误控制台? Where is the { on the first line of your JavaScript matched? JavaScript第一行中的{在哪里匹配?

First, I've never used mysqli. 首先,我从未使用过mysqli。 Does mysqli_query work with mysql_fetch_array ? mysqli_query是否可以与mysql_fetch_array

In any case, one potential problem I see is here: 无论如何,我在这里看到一个潜在的问题:

while ($row = mysql_fetch_array($r)) {
    $arr=array('cname'=>$cname, 'caddress'=>$caddress);
    echo json_encode($arr);
}

That won't be a correct json unless the result count is 1. For example if you get two results you'll generate an incorrect JSON: 除非结果计数为1,否则它将不是正确的json。例如,如果获得两个结果,则将生成错误的JSON:

{"cname":"CNAME1","caddress":"CADDRESS1"}{"cname":"CNAME2","caddress":"CADDRESS2"}

If you need to read multiple rows, the loop should be: 如果需要读取多行,则循环应为:

$arr = array();    
while ($row = mysql_fetch_array($r)) {
    //...
    $arr[] = array('cname'=>$cname, 'caddress'=>$caddress);
}
echo json_encode($arr);

But then the javascript code will get an array back, rather than an object, so you'll have to change it too. 但是,然后javascript代码将返回一个数组,而不是一个对象,因此您也必须更改它。

If this doesn't solve your problem, add some more info/code to your question so that we could understand it better 如果这样做不能解决您的问题,请向您的问题添加更多信息/代码,以便我们更好地理解

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

相关问题 使用ajax根据组合框值填充文本框 - populate textbox based on combobox value using ajax 如何使用 jquery 设置文本框数组元素的值? - How do I set the value of a textbox array element using jquery? 如何在Chrome和Firefox以及IE中显示最近输入的文本框值? - How do I show recently entered textbox value in Chrome and Firefox also IE? 我如何发布由jQuery动态创建的文本框值 - how do i post textbox value which is dynamically created by jquery 我如何将输入文本框值的参数传递给jQuery中的Ajax - How do i pass parameters that is input textbox value to ajax in jQuery 如何基于上一个在php中输入的值来限制在文本框中输入的值 - how to restrict the value entered in a textbox based on the value entered in the previous one in php 如何使用ajax根据其先前的文本框值填充文本框 - How to fill a textbox based on its previous textbox value using ajax 我如何使用一个文本框中具有多个标签的jQuery做一个文本列表 - How can i do a textbox list using jquery having multiple tag in one textbox 如何在第一个 select 框的基础上自动填充第二个 select 框并在第二个 select 框的基础上自动填充文本框 - How to auto populate second select box on base of first select box and auto populate textbox on base of second select box 如何打印上面文本框中输入的相同值 - how to print same value as entered in above textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM