简体   繁体   English

从组合框PHP更改值时更改价格

[英]Change the price when value change from combo box PHP

I have a problem with my code. 我的代码有问题。 I want to change the price when the value of the combo box changes, but in the while-loop handler, only the first table row is processed. 我想在组合框的值更改时更改价格,但是在while循环处理程序中,仅处理第一表行。

<script type="text/javascript">
    $(document).ready(function () {
        $('#size').change(function () {
            //alert("aaa");
            var x = $('#size').attr('value');
            //alert(x);
            $.ajax({
                type: "POST",
                url: 'sizeChange.php',
                data: 'size =' + size,
                success: function (data) {
                    alert(data);
                }
            })
        });
    });
</script>



<div id="price">
    <table border="1" width="200px">
     <tr>
        <td> Size </td>
        <td> Product</td>
        <td>Price</td>
     </tr>
    <?
       mysql_connect("localhost","root","") or die(mysql_error()); 
       mysql_select_db("test") or die(mysql_error());
       $query = mysql_query("select * from pricetest");
       if(!$query)
         { die(mysql_error());}
         else
        {
         while($row = mysql_fetch_array($query))
          {
            echo "<tr> ";
                ?>
             <td>
                 <select name="size" id="size">
                   <option value="1">1</option>
                   <option value="2">2</option>
                    <option value="3">3</option>
                  </select>
             </td>
             <td> <? $row['pd_name']; ?> </td>
             <td> <? $row['price']; 

                    }
                }?>
             </td>
        </tr>
      </table>
</div>

What might I being doing wrong? 我可能做错了什么?

You can use var x = $('#size').val(); 您可以使用var x = $('#size').val(); instead of var x = $('#size').attr('value'); 而不是var x = $('#size').attr('value'); Also send data as data: 'size =' + x in ajax. 还以data: 'size =' + x发送数据data: 'size =' + x ajax中的data: 'size =' + x

$(document).ready(function () {
    $('#size').change(function () {
        //alert("aaa");
        var x = $('#size').attr('value'); alert(x);
        //alert(x);
        $.ajax({
            type: "POST",
            url: 'getsize.php',
    data: 'size='+x,
            success: function (data) {
                alert(data);
            }
        })
    });
});

It's because all your elements have the same id ( size ). 这是因为您所有的元素都具有相同的id( size )。

Change your php code: 更改您的PHP代码:

$i = 1;
while($row = mysql_fetch_array($query))
{
    echo "<tr id='row".$i."'> ";
            ?>
     <td>
             <select name="size" class="size_dd" id="<?php echo $i ?>" >
                 <option value="1">1</option>
                 <option value="2">2</option>
                    <option value="3">3</option>
                </select>
     </td>
     <td class="pd_name" > <? $row['pd_name']; ?> </td>
     <td class="price" > <? $row['price']; ?> </td>
     <?php $i++ ?>
}

and change the JS to something like this: 并将JS更改为如下所示:

<script type="text/javascript">
    $(document).ready(function () {
        $('.size_dd').change(function () {
            //alert("aaa");
            var size = this.value,
                                id = this.value;
            //alert(x);
            $.ajax({
                type: "POST",
                url: 'sizeChange.php',
                data: 'size =' + size,
                success: function (data) {
                    $('tr#row td.pd_name').html(data['pd_name']);
                    $('tr#row td.price').html(data['price']);
                }
            })
        });
    });
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM