jQuery select value not set
I am trying to set an action for when a select value is between 0 and 2, but with the function below, I am getting the action triggering when the value is not defined. What is the best method for doing so?
$('#html_s开发者_开发问答elect').click(function(){
var option = $(this).val();
if(option < 3 && option != null){
//Do something
}
});
What you are doing is perfectly fine.
It is usually better to use the onchange
method:
$('#html_select').change(function(){
var option = this.value; //DOM's value
if(option < 3 && option != null){
//Do something
}
});
Fiddle: http://jsfiddle.net/maniator/pFFAH/
精彩评论