【问题标题】:data received through JQuery, php, mysql is not working通过 JQuery、php、mysql 接收的数据不起作用
【发布时间】:2015-02-18 03:19:53
【问题描述】:

在文本框中输入客户姓名后,它会搜索客户信息。我通过 Json 变量传递整个表,使用 JQuery 成功生成,因为我不想要任何页面刷新。现在我想通过单选按钮选择从 mysql db (php) 生成的客户 ID,但单选按钮事件不起作用。出于测试目的,我在该特定 div 中放置了一个具有相同单选按钮属性的静态表(使用 JQuery 生成动态记录的位置)并且工作正常。因此我发现通过 JQuery 接收到的数据有问题。希望我清楚。请想办法。提前致谢。

下面是代码

abc.php

<input type="text" placeholder="full name" id="fullName" name="fullName" class="txt" style="width: 250px" />&ensp;
<input type="button" id="btSelect" value="Select" class="button-crystal" />
<div id="disp"></div>

script.js

$('#btSelect').click(function () {
        var form_data = {
            genCustDetails: $('#fullName').val(),
            is_ajax: 1
        };

        $.ajax({
            type: "POST",
            url: "xyz.php",
            data: form_data,
            dataType: "json",
            success: function (response)
            {
                $('#disp').html(response);
            }
        });
        return false;        
    });

xyz.php

if (isset($_POST['genCustDetails'])) {
    $out="";
    $result = mysql_query("select * from dbcustdetails where name like '$_POST[genCustDetails]%'");
    while ($row = mysql_fetch_assoc($result)) {
        $out.='
        <table style="background-color:#eee; margin-bottom:5px;">
            <tr>
                <th class="td3">Customer ID</th>
                <td class="td4">
                    '.$row["custID"].'&ensp;<input type="radio" id="cust_ID" name="cust_ID" value="'.$row["custID"].'" />
                </td>
            </tr>
            <tr>
                <th class="td3">Name</th>
                <td class="td4">'.$row["name"].'</td>
            </tr>
            <tr>
                <th class="td3">Phone No.</th>
                <td class="td4">'.$row["phno"].'</td>
            </tr>
            <tr>
                <th class="td3">Email</th>
                <td class="td4">'.$row["email"].'</td>
            </tr>
            <tr>
                <td colspan="2" style="padding:0;">
                    <b>Address</b><br/>'.$row["addr"].'
                </td>
            </tr>
        </table>';       
    }
    echo json_encode($out);
}

【问题讨论】:

  • 您可能必须使用 (on)[api.jquery.com/on/] 或其他方式将您的广播事件绑定到 ajax 响应

标签: php jquery mysql


【解决方案1】:

也许您不应该为 DOM 中的动态元素正确绑定事件。

这样试试

$('body').on('change','.radiobuttonClassorID',function(){
    //actions
});

【讨论】:

    【解决方案2】:

    这是因为您新生成的单选按钮没有分配任何事件处理程序。

    您必须在 ajax 调用之后分配一个事件处理程序(在 ajax 成功时)。 像

    $('input[type="radio"]').unbind('click').click(function(){
        //Your handler code
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多