简体   繁体   中英

Javascript - How to change a new value on drop down

When we change the name="quantity" and the $product['price'] value will changing too. Here will have dynamic quantity and price. How to do that using jquery/javascript.

<?php
$select  = "SELECT * FROM `products` ORDER BY `id` DESC LIMIT 10"; 
$query  = mysql_query($select) or die(mysql_error());
?>

<ul>
  <?php while($product = mysql_fetch_array($query)) { ?>
  <li>
    <p><?php echo $product['name'];?></p>
    <p> Quantity
      <select name="quantity">
        <?php  for($i=1;$i<=$product['quantity'];$i++) { ?>
        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
        <?php } ?>
      </select>
   </p>
   <p><?php echo $product['price'];?></p>
 </li>
<?php } ?>
</ul>

Let me know :)

Using jQuery:

$(document).ready(function() {
    $("#quantity_select").bind("change", function() {
        selected_option = $("option:selected").val();
        $("#counter").html(selected_option);
    });
});

...

<select id="quantity_select">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

<div id="counter"></div>

You can use text() instead of val() if you want to see actual text instead of value attribute .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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