更改事件不会在ajax加载的元素上触发

时间:2017-06-02 11:13:21

标签: javascript php jquery ajax

当我点击第一个装入ajax的项目时,第二个ajax调用更改事件无法正常工作。我怀疑我的$ name1是否在第二个ajax data:{name1: name1},

下拉

<select id="name">
  <option style="display:none;" selected>Select Product</option>
</select>

当变量位置可用时,第一个ajax调用将填充下拉框

<?php if (isset($_GET['place']) && $_GET['place'] != '') { ?>

    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script>
        $.ajax({
            type: "POST",
            data: {place: '<?= $_GET["place"] ?>'},
            url: 'listplace.php',
            dataType: 'json',
            success: function (json) {
                if (json.option.length) {
                    var $el = $("#name"); 
                    $el.empty(); // remove old options
                    $el.append('<option selected>Select language value</option>');
                    for (var i = 0; i < json.option.length; i++) {
                        $el.append($('<option>',
                            {
                                value: json.option[i],
                                text: json.option[i]
                            }));
                    }
                }else {
                    alert('No data found!');
                }
            }
        });
    </script>

//单击选项时继续第二次ajax调用,它将列出产品详细信息

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$("#name").on('change',function (e) { 
    var name1 = this.value;
    $.ajax ({
        data:{name1: name1},
        type: 'POST',
        url: 'dataprod.php',
        success: function (response) {
            console.log(response);
            $('.products-wrp').html('');
            $('.products-wrp').hide();
            $('.products-wrp').html(response);
            $('.products-wrp').show();            
        },
    });
});
</script>     
<?php } ?>

dataprod.php

<?php
$name1 = $_POST['name1'];
$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code,  
product_image, product_price FROM products_list where product_name='$name1'");
$products_list =  '<ul id ="products_list" class="products-wrp">';
while($row = $results->fetch_assoc()) {
$products_list .= <<<EOT
<li>
<form class="form-item">
<h4>{$row["product_name"]}</h4>
<div>
<img src="images/{$row["product_image"]}" height="62" width="62">
</div>
<div>Price : {$currency} {$row["product_price"]}<div>
</form>
</li>
EOT;
}
$products_list .= '</ul></div>';
echo $products_list;
?>  

0 个答案:

没有答案