简体   繁体   中英

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

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

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