2017-05-29 120 views
0

我想從數據庫中獲取文本框的值取決於下拉列表值,而不使用提交按鈕。現在,我的code.how上有三個下拉列表我可以寫三個下拉菜單的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> 
+0

您可以通過創建事件偵聽器來實現此目的,例如失去焦點,然後使用select的數據獲取值。 –

回答

0

我推薦你使用jQuery開始。操縱DOM更容易。 按照教程進行操作,然後在下拉菜單上對更改事件附加回調,然後在回調中獲取ajax調用中菜單的值,然後生成html(選項標籤)並更新您的選擇。

Ajax調用和html更新可以使用jQuery完成,但您也可以在純Javascript中自己完成。

相關問題