更新文本框的值仅从第一个文本框获取值

时间:2014-01-09 11:15:50

标签: javascript jquery textbox

我有购物车页面,其中有文本框用于更新数量,我正在使用jquery来收听点击事件&获得productid&的价值文本框。 Productid我变得很好但文本框的值总是第一个文本框中的值。

这是购物车档案的代码

echo "<tr>";
            echo "<th><img src=".$row["image_location"]."></th>";
            echo "<th>".$row["product_name"]."</th>";
            echo "<th><input type='text' id='quantity' name='quantity' required='required' autocomplete='off' value=".$row["quantity"].">";
            echo "&nbsp";
            echo $row["type"]."</th>";
            echo "<th>".$row["price"]."</th>";
            echo "<th>"."&#8377;&nbsp;".$subtotal."</th>";
            echo "<th><div class='buttoncircle' id='".$row["productid"]."'>Update</div></th>";
            echo "<th><a href='removefromcart.php?productid={$row["productid"]}' class='buttoncircle' style='margin-left:-65px'>Remove</a></th>";
        echo "</tr>";

以下是javascript的代码。

<script>
$('.buttoncircle').live('click',function() {
    var productid = $(this).attr('id');
    var quantity = $('#quantity').val();
    window.open('db_changequantity.php?quantity='+quantity+'&productid='+productid,'_self');
});

任何人都可以帮助调用特定文本框的价值。不仅仅是第一个?

1 个答案:

答案 0 :(得分:1)

为数量归档添加动态ID - id='quantity_".$row["productid"]."'并在javascript var quantity = $('#quantity_'+productid).val();中映射相同内容。

在PHP中

    echo "<tr>";
    echo "<th><img src=".$row["image_location"]."></th>";
    echo "<th>".$row["product_name"]."</th>";
    echo "<th><input type='text' id='quantity_".$row["productid"]."' name='quantity' required='required' autocomplete='off' value=".$row["quantity"].">";
    echo "&nbsp";
    echo $row["type"]."</th>";
    echo "<th>".$row["price"]."</th>";
    echo "<th>"."&#8377;&nbsp;".$subtotal."</th>";
    echo "<th><div class='buttoncircle' id='".$row["productid"]."'>Update</div></th>";
    echo "<th><a href='removefromcart.php?productid={$row["productid"]}' class='buttoncircle' style='margin-left:-65px'>Remove</a></th>";
    echo "</tr>";

使用Javascript:

    $('.buttoncircle').live('click',function() {
    var productid = $(this).attr('id');
    var quantity = $('#quantity_'+productid).val();
    window.open('db_changequantity.php?quantity='+quantity+'&productid='+productid,'_self');
});
相关问题