jquery datepicker change value of input, based on difference between two dates
I have 3 input fields
<input type="text" name="cust1" id="cust1" size="10" value="" class="inputbox" />
<input type="text" name="cust2" id="cust2" size="10" value="" class="inputbox" />
<input type="text" name="rent_price" id="rent_price" size="10" value="" disabled="disabled" class="inputbox" />
jQuery Datepicker
$( "input#cust1" ).datepicker();
$( "input#cust2" ).datepicker();
And function to change value of input#rent_price:
function setSum()
{
var hrs = countHrs();
$("input#rent_price").attr('value',function(){
sum = parseInt(BAG_PRICE)*parseInt(hrs);
if (isNaN(sum))
{
sum = '';
return sum;
}
else
{
return sum;
}
});
}
so when i'm writind this:
$( "input#cust1" ).change(setSum());
$( "input#cust2" ).change(setSum());
开发者_JAVA技巧
value of input#rent_price changing only afted refreshing page. If i write alert in setSum()
it triggers 2 times. So i need some advice.
use the datepicker's onClose event:
$( "input#cust1" ).datepicker({onClose:function(){ setSum() }});
$( "input#cust2" ).datepicker({onClose:function(){ setSum() }});
also when writing code, try not to repeat yourself, editted your function a bit:
function setSum()
{
var sum = parseInt(BAG_PRICE)*parseInt(countHrs());
if (isNaN(sum)) { sum = '' }
$("input#rent_price").val(sum);
}
精彩评论