简体   繁体   中英

i want to display previous month balance amount by clicking a month in select box

when i select a month in selectbox ,automatically previous month balance should display in the below text box. i have db table like this:

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

if i click jan 2015 in select box i want to get prev month amount 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();?>

I outlined the base idea in this fiddle

Basically you need to get the current selected month and year every time. So do something on

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);    

});

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