[英]i want to display previous month balance amount by clicking a month in select box
當我在selectbox中選擇一個月份時,上一個月的余額會自動顯示在下面的文本框中。 我有這樣的數據庫表:
month: year amount:
January 2015 2000
February 2015 3000:
dec: 2015 4000
dec: 2014 5000
如果我在選擇框中單擊Jan 2015,我想獲得上個月的金額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();?>
我在這個小提琴中概述了基本思想
基本上,您需要每次獲取當前選定的月份和年份。 所以做點什么
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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.