我想通过单击选择框中的月份来显示上个月的余额金额

时间:2015-08-10 05:20:45

标签: jquery codeigniter-routing

当我在选择框中选择一个月时,自动上个月余额应显示在下面的文本框中。 我有这样的db表:

month:  year    amount:   
January  2015    2000
February   2015   3000:
dec:        2015   4000
dec:        2014   5000

如果我点击选择框中的2015年1月我希望获得上个月金额5000:

 <?php 
    $usr = $this->session->userdata('usr');
    echo form_open('money_c/addprevmonth');
?>
        <table border="0" style="margin-left:20px">
        <tr>
            <td>Month:</td>
            <td>
                <select name="month" style="margin-left:6px;">
                <option value="January">January</option>
                <option value="February">February</option>
                <option value="March">March</option>
                <option value="April">April</option>
                <option value="May">May</option>
                <option value="June">June</option>
                <option value="July">July</option>
                <option value="August">August</option>
                <option value="September">September</option>
                <option value="November">November</option>
                <option value="December">December</option></select>
            </td>
        </tr>
     <tr>
         <td>Year:</td>
         <td>
              <select name="year" style="margin-left:7px;    margin-top: 26px;">
          <?php  for($i=1990;$i<3000;$i++) {
                        echo "<option>".$i."<option>" ;
                        }
                ?>
                  </select>
             </td>
        </tr> 
     <tr>
         <td>Previous Month Balance:</td>
         <td><input type="text" value".iwant to display here....." ></td>
     </tr>
        <tr>
            <td>Cash in hand:</td>
            <td><input type="text" class="text" ></td>
        </tr>
   </div>
 </table>
  <div class="p-container">
     <!--<label class="checkbox"><input type="checkbox" name="checkbox" checked><i> </i>I Agree to the terms of use</label>-->
  </div>
    <input type="submit"  value="Add" >
    <?php echo form_close();?>

1 个答案:

答案 0 :(得分:0)

我在this小提琴

中概述了基本思想

基本上,您需要每次都获得当前选定的月份和年份。所以在

上做点什么
var arrayOfMonths = [];

$("select[name='month'] > option").each(function()
{
    // Adds the value of the option to the array
    arrayOfMonths.push($(this).val());
});

$('select').on('change', function() {  
    //get selected month
    var month = $( "select[name='month']").find(":selected").text();
    var positionOfMonthInArray = $.inArray(month, arrayOfMonths);
    var previousMonthIndex = positionOfMonthInArray-1;
    //get selected year
    var year = $( "select[name='year']").find(":selected").text();

    if(previousMonthIndex == -1){
        //if index of month is -1 selected the last month
         previousMonthIndex = arrayOfMonths.length - 1;
         year = year - 1;
    }


//Do what you want here...
        alert(arrayOfMonths[previousMonthIndex]);
        alert(year);    

});