views:

31

answers:

2

I have to sets of dates (via inputs/selects - mm/dd/yyy) on page. I want certain readonly text field to show age in months (calculated based on those two dates) after a certain select field (different select, not a part of dates set) has been changed (any option in that select chosen must trigger calculation and show result in readonly field). It should be pretty simple, but doesn't work, would appreciate any advise. This is what I have so far:



    $(document).ready(
    function (){


    // bind the recalc function to the select field
    $("#certain_select").bind("change", recalc);
    // run calculation 
    recalc();


    }
    );


function recalc(){  

var b_day=parseInt($("#dob_dd").value);
var b_mo=parseInt($("#dob_mm").value);
var b_year=parseInt($("#dob_yy").value);


var visit_day=parseInt($("#visit_dd").value);
var visit_mo=parseInt($("#visit_mm").value);
var visit_year=parseInt($("#visit_yy").value);


var age;
if (visit_year==b_year){
age=(visit_mo-b_mo);
$("#showage").text(age.toString());
}else if(b_year<visit_year){
var age_years=(visit_year-b_year);
var age_months=(visit_mo-b_mo);
age=(12*age_years + age_months);
$("#showage").text(age.toString());
}else{
age=0;
$("#showage").text(age.toString());
}



    }//end of recalc function





A: 

For starters, switch your instances of $("#the_id").value to $("#the_id").val() - one for each of your date-related boxes (6 places).

Everything else looks like it should work, but it's hard to say without an idea of what you mean by "doesn't work".

BBonifield
A: 

HTML is straight forward.

<select name="visit_mm" id="visit_mm" class="required">
<option value=""> </option>
<option value="">1</option>...
</select>

<select name="visit_dd" id="visit_dd" class="required">
<option value=""> </option>
<option value="1">1</option>
...
<option value="12">12</option>
</select>

<input type="text" name="visit_yy" id="visit_yy" size="4" maxlength="4" value="" class="required">

...same for dob fields, only different ids.

<select name="dob_mm" id="dob_mm" class="required">
<option value=""> </option>
<option value="1">1</option>
...
<option value="12">12</option>
</select>

<select name="dob_dd" id="dob_dd" class="required">
<option value=""> </option>
<option value="1">1</option>
...
<option value="31">31</option>
</select>

<input type="text" name="dob_yy" id="dob_yy" size="4" maxlength="4" value="" class="required">

Readonly field goes like this:

<input type="text" readonly class="readonly" name="delayed_start" id="delayed_start" size="4" maxlength="4" value="">

and there's a select with id="recommended_visit"

<select name="recommended_visit" id="recommended_visit" class="required">
<option value=""> </option>
</select>

When value of thats select changes, age, calculated using visit and dob fields, should be displayed in readonly field. Making sure the date is valid (based on how many days in certain months, chekcing for leap years and such) is not needeed at all. any date will do.

AR
By doesn't work I mean nothing shows in readonly field when I pick anything from "recommended_visit" select field.
AR
Ok, I figured it out, I've used text() and it doesn't work on input fields. Switched to val(), now everything's fine.
AR