简体   繁体   English

如何从数据库获取文本框中的值取决于没有提交按钮的下拉列表

[英]How to get the value in Textbox from database depends on dropdown list without submit button

I want to get the text box value from database depends upon the dropdownlist values without using submit button .Now I have three dropdown list on my code.how can i write the javascript for three dropdown 我想从数据库中获取文本框的值取决于dropdownlist的值而不使用提交按钮。现在我的代码上有三个下拉列表。如何为三个下拉列表编写JavaScript

<div align="center">
<table>
<thead>
<th>Product</th>
<th>Quantity</th>
<th>Rate</th>
<th>Amount</th>
</thead>
<tbody>
    <tr id="addrow">
    <td>
    <?php
    $selectproduct=mysql_query("select productname from items");
    $itemname=mysql_fetch_array($selectproduct);
    ?>
    <select name="item[]" id="item">

    <?php
    $i=0;
    while($itemname=mysql_fetch_array($selectproduct))
    {
    ?>
      <option value="<?php echo $itemname['productname'];?>"><?php echo $itemname['productname'];?></option>
      <?php
     $i++;
    }
    ?>
    </td>
    <?php

 //    $amount=mysql_query("select Amount from items where Productname='$productname' ");
   //  $totalamount=mysql_fetch_array($amount);
    ?>


    <td><input class="qty" type="text" name="qty[]"></td>
    <td><input class="price" type="text" name="price[]"></td>
    <td><input type="text" class="output" name="output[]"></td>
    </tr>
    <tr >
    <td>
    <?php
    $selectproduct=mysql_query("select productname from items");
    $itemname=mysql_fetch_array($selectproduct);
    ?>
    <select name="item[]" id="item">

    <?php
    $i=0;
    while($itemname=mysql_fetch_array($selectproduct))
    {
    ?>
      <option value="<?php echo $itemname['productname'];?>"><?php echo $itemname['productname'];?></option>
      <?php
     $i++;
    }
    ?>
    </td>

    <td><input class="qty" type="text" name="qty[]"></td>
    <td><input class="price" type="text" name="price[]"></td>
    <td><input type="text" class="output" name="output[]"></td>
    </tr>
    <tr>
    <td>
    <?php
    $selectproduct=mysql_query("select productname from items");
    $itemname=mysql_fetch_array($selectproduct);
    ?>
    <select name="item[]" id="item">

    <?php
    $i=0;
    while($itemname=mysql_fetch_array($selectproduct))
    {
    ?>
      <option value="<?php echo $itemname['productname'];?>"><?php echo $itemname['productname'];?></option>
      <?php
     $i++;
    }
    ?>
    </td>

   <td><input class="qty" type="text" name="qty[]"></td>
    <td><input class="price" type="text" name="price[]"></td>
    <td><input type="text" class="output" name="output[]"></td>
    </tr>
</table>
<div id="grand">
Total Amount:<input type="text" name="gran" id="gran">
</div>
    </tr>
    <tr >
    <td colspan="4">
    <center><input type="submit" name="submit"></center>
    </td>
    </tr>
</tbody>
</table>
</div>
</form>
</body>
<script type="text/javascript">
    $(document).ready(function() {
    $(".price").keyup(function() {
        var grandTotal = 0;
        $("input[name='qty[]']").each(function (index) {
            var qty = $("input[name='qty[]']").eq(index).val();
            var price = $("input[name='price[]']").eq(index).val();
            var output = parseInt(qty) * parseInt(price);

            if (!isNaN(output)) {
                $("input[name='output[]']").eq(index).val(output);
                grandTotal = parseInt(grandTotal) + parseInt(output);    
                $('#gran').val(grandTotal);
            }
        });
    });
}); 
</script>

I recommand you to use jQuery for start. 我建议您使用jQuery作为开始。 It is easier to manipulate the DOM. 操作DOM更容易。 Follow a tutorial and then, attach a callback on the change event on your dropdown menus, then in the callback get the values for your menu in an ajax call, then generate the html (the option tags) and update your select. 按照教程进行操作,然后在下拉菜单上的change事件上附加一个回调,然后在该回调中通过ajax调用获取菜单的值,然后生成html(选项标签)并更新您的选择。

The Ajax call and the html update can be done with jQuery, but you can do it by yourself in pure Javascript as well. Ajax调用和html更新可以使用jQuery完成,但是您也可以自己使用纯Javascript完成。

You'll need to use AJAX calls from JS. 您需要使用来自JS的AJAX调用。 AJAX has to send and receive data from php script. AJAX必须从php脚本发送和接收数据。 Simple AJAX tutorial: https://www.w3schools.com/js/js_ajax_php.asp 简单的AJAX教程: https : //www.w3schools.com/js/js_ajax_php.asp

暂无
暂无

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

相关问题 获取选定的下拉列表而不提交按钮 - Get Selected Value of dropdown without submit button 获取没有提交按钮的选定下拉值 - get selected dropdown value without submit button 从下拉列表中将选定的值分配给PHP变量onchange,而无需按html表单中的“提交”按钮? - Assigning a selected value from a dropdown list to a php variable onchange, without pressing submit button in a html form? 从下拉菜单中选择带有下拉菜单的数据库,而无需提交按钮 - Load records from database with dropdown selection without submit button 在JavaScript中没有提交按钮的情况下从文本框中获取信息? - Get info from textbox without submit button in JavaScript? 用户从列表中选择一个选项而不单击提交按钮后,如何在页面中显示文本框或选择/选项? - How to show a textbox or select/option in the page once user selects an option from the list without clicking the submit button? 根据下拉列表选择值从数据库中获取下拉选择值到两个文本框,IDIN MVC4 jQuery - Get dropdown selected value to two textbox from database based on dropdown list selected value idin mvc4 jquery 如何在不单击按钮的情况下将价值带入文本框 - how to get value into textbox without button click 根据下拉列表值的更改从数据库自动填充文本框 - Autofill textbox from database based on change of dropdown list value 当使用 javascript 或 jQuery 从填充的文本框中选择任何值时如何提交表单而不单击提交按钮 - How to submit form when any value selected from populated textbox using javascript or jQuery without clicking submit button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM