简体   繁体   中英

Multiplying a value chosen by drop down and a constant with ajax

My problem is that i need to use the value that the user selected and a constant that doesn't mater to multiply them and to show the result in an text-box with ajax

<select name="service" style="width: 230px; margin-left: 30px; height: 30px;margin-bottom:15px;">
    <?php 
      $sql = mysqli_query($mysqli, "SELECT * from service_tbl");
      while ($row = $sql->fetch_assoc()){
        echo "<option value=\"service1\">" . $row['serviceName'] . "</option>";
      }
    ?>
</select>

This is the drop down list that i am getting the value from

<input type="text" name="total" id = "output"  readonly="" style="margin-left: 62px;margin-bottom:15px;width: 230px; height: 30px;"><br>

this is the text box that needs to update when the drop down list changes value.

I hope you can help me.

Here's an example without PHP. The AJAX call returns the constant.

 var multiplier; $.getJSON("jsonResponse.php").done(function(response) { multiplier = response.constant; // simulate AJAX JSON response: {"constant": 1000} }) .fail(function() { multiplier = 1000; // jsonResponse.php doesn't exist so this will run }) $('#service').on('change', function() { var num = $('#service').val(); if(num) { $('#output').val(num * multiplier); } else { $('#output').val(''); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="service"> <option value="">-- select a service --</option> <option value="124">Service 124</option> <option value="875">Service 875</option> </select> <input type="text" name="total" id="output" readonly> 

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