Google Chrome - $('#staylength').val(my_min); fails
HTML:
<select id="staylength" name="staylength">
<option disabled="disabled" value="1">1</option>
<option disabled="disabled" value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
In Google Chrome (5.0.375.55 on WinXP), the following jquery code fails t开发者_C百科o set the value of staylength
.
var my_min = 3;
$('#staylength').val(my_min);
But the same, when run from javascript console in Google Chrome, is successful. What Is wrong?
Edit
Here is the offending page: http://dv2.makestay.com/node/1To reproduce the error select either '06/21/2010' or '06/22/2010' as Check-In Date. The value failing to get updated is in Nights
select.
EDIT 2
This is the whole function. It is called with parameter set to 3:
function apply_restriction(i) {
var my_index = parseInt(i);
var my_option;
var my_min = min_stay[my_index];
$('#staylength').html('');
for (var j=1; j < 15; j++) {
if (parseInt(j) < parseInt(my_min)) {
my_option = '<option value="' + j + '" disabled="disabled">' + j + '</option>';
} else {
my_option = '<option value="' + j + '">' + j + '</option>';
}
$('#staylength').append(my_option);
}
$('#staylength').val('' + my_min);
alert($('#staylength').length);
}
Updated
The issue is this line:
var my_min = min_stay[i] + 1;
min_stay
is an array of strings, so this is resulting in "31", not 4
like you want (and "31" is outside the values in the list, resulting it it defaulting back to the first <option>
), you either need to return an array of integers in your response, instead of this:
min_stay.push('3', '3');
//should be:
min_stay.push(3, 3);
or, use parseInt()
like this:
var my_min = parseInt(min_stay[i], 10) + 1;
Try using a string rather than a number.
var my_min = "3";
$('#staylength').val(my_min);
try
$('#staylength option[value='+my_min+']').attr('selected','selected');
// instead of $('#staylength').val('' + my_min);
but would be nice if you do this (requirement is Jquery 1.4 )
function apply_restriction(i) {
var my_index = parseInt(i);
var my_option;
var my_min = min_stay[my_index];
$('#staylength option').each(function(){
$(this).attr('disabled',function(){
return this.value < my_min;
});
});
$('#staylength option[value='+my_min+']').attr('selected','selected');
alert($('#staylength').length);
}
精彩评论