doudao2954 2015-08-11 04:44
浏览 55
已采纳

Jquery javascript下拉循环和计算

I got a a dropdown list populating from a php while loop. There will be more than one select dropdown boxes. When i select any dropdown from the loop, the subsequent textbox should change the value.

<html>
   <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title></title>
      <script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>
      <script type='text/javascript'>//<![CDATA[
         $(window).load(function(){
         $("#category").change(function () {
         var dept_number = $(this).val();
         var price = $(this).find(':selected').data('price');
         $('#dept-input').val(dept_number);
         $('#price-input').val(price);
         });
         });//]]> 


      </script>
   </head>
   <body>
      <select id="category[]">
         <option value="1" data-price="10">Item 1 second</option>
         <option value="2" data-price="20">Item 2 second</option>
         <option value="3" data-price="30">Item 3 second</option>
      </select>
      <br><br>
      <label>Dept. num:</label>
      <input type="text" id="dept-input[]"></input>
      <br><br>
      <label>Price:</label>
      <input type="text" id="price-input[]"></input>
   </body>
</html>

This script is working with out the loop. How will i change it to work with the loop.. Thank you in advance

<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function(){
    $("select").on('change',function () {
        var dept_number = $(this).val();
        var price = $(this).find(':selected').data('price');
        $(this).siblings('.deptip').val(dept_number);
        $(this).siblings('.priceip').val(price);
        $(this).siblings('.total1').val(price * $(this).siblings('.total').data('value'));
    });
});
});//]]> 

</script>
<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
    <td><select id="category0">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select></td>
    <td><label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
    <td><label>Price:</label>
    <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
    <td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
    <td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>

</tr></table>
</div>

<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
    <td><select id="category1">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select></td>
    <td><label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
    <td><label>Price:</label>
    <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
    <td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
    <td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>

</tr></table>
</div>

<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
    <td><select id="category2">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select></td>
    <td><label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
    <td><label>Price:</label>
    <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
    <td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
    <td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>

</tr></table>
</div>

</div>
  • 写回答

2条回答 默认 最新

  • duananyu9231 2015-08-11 05:07
    关注

    DEMO

    • Categorize your multiple selects into some parent div container and assign class to input elements in each container

    For ex:

    <div class="base"> <!--Keep each group in base container-->
        <select id="category">
           <option value="1" data-price="10">Item 1 second</option>
           <option value="2" data-price="20">Item 2 second</option>
           <option value="3" data-price="30">Item 3 second</option>
        </select>
        <label>Dept. num:</label>
        <input type="text" class="deptip" id="dept-input"/><!--give them a class-->
        <label>Price:</label>
        <input type="text" class="priceip" id="price-input"/> <!--a class here too-->
    </div>
    
    <div class="base">
        <select id="category1">
           <option value="1" data-price="10">Item 1 second</option>
           <option value="2" data-price="20">Item 2 second</option>
           <option value="3" data-price="30">Item 3 second</option>
        </select>
        <label>Dept. num:</label>
        <input type="text" class="deptip" id="dept-input"/><!--same goes here-->
        <label>Price:</label>
        <input type="text" class="priceip" id="price-input"/><!--and here too-->
    </div>
    
    <div class="base">
        <select id="category2">
           <option value="1" data-price="10">Item 1 second</option>
           <option value="2" data-price="20">Item 2 second</option>
           <option value="3" data-price="30">Item 3 second</option>
        </select>
        <label>Dept. num:</label>
        <input type="text" class="deptip" id="dept-input"/>
        <label>Price:</label>
        <input type="text" class="priceip" id="price-input"/>
    </div>
    
    • Write a common jquery on change event to select element.

    For Ex:

    $(document).ready(function(){ //Write it in document.ready()
        $("select").on('change',function () {
            var dept_number = $(this).val();
            var price = $(this).find(':selected').data('price');
            $(this).siblings('.deptip').val(dept_number); //find its siblings with classname 
            $(this).siblings('.priceip').val(price);
        });
    });
    

    Update

    DEMO

    Hoping the html will be there for each group

    <label>Total:</label><input data-value="50" type="text" readonly class="total"/><br/>
    <!-- Added value fetched from php to data-value attribute of input-->
    

    So your modified JS would be as below:

    $(document).ready(function(){
        $("select").on('change',function () {
            var dept_number = $(this).val();
            var price = $(this).find(':selected').data('price');
            $(this).siblings('.deptip').val(dept_number);
            $(this).siblings('.priceip').val(price);
            $(this).siblings('.total').val(price * $(this).siblings('.total').data('value'));//Add this line
        });
    });
    

    UPDATE 2

    DEMO

    For your current structure you could change your script as below:

    $(document).ready(function(){
        $("select").on('change',function () {
            var dept_number = $(this).val();
            var price = $(this).find(':selected').data('price');
            $(this).closest('table').find('.deptip').val(dept_number);
            $(this).closest('table').find('.priceip').val(price);
            $(this).closest('table').find('.total').val(price * $(this).closest('table').find('.total').data('value'));
        });
    });
    

    Basically here we are finding the parent table of the clicked select and then finding required elements within that parent

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上