MDIT
MDIT

Reputation: 1548

Jquery change() dont work on select box when contain only one item

I'm working on a dynamic select box with php and jquery, i have 4 select boxes, the content of the first one is loaded in the page load.

When i choose one of the items in the first select box, the seconde one get data from database related to the first select box...and so on

But the problem is : when the select box contain only one item, the change() function of jquery doesn't work $('#MySelectId').change(function(){ //......});

Dynamic Dependent Select Box Using jQuery and Ajax

Any suggestions ?

I'm working with PHP, jQuery v1.6.4 and jQuery Chosen Plugin.

Upvotes: 0

Views: 562

Answers (2)

Anand Jha
Anand Jha

Reputation: 10724

Try something like

$('#MySelectId').on('change', 'select', function() {
  alert('changed');
  }).click(function(){
      if($('#MySelectId select option').length == 1) {
      $('#MySelectId select').change();
      }
});

Fiddle

or you can try with jquery bind if you are not using 1.7.x + version.

$('#MySelectId').bind('change', function() {
  alert('changed');
}).click(function(){
   if($('#MySelectId select option').length == 1) {
    $('#MySelectId select').change();
}
});

test here

check this post also

Upvotes: 1

sobyte
sobyte

Reputation: 96

If your select box only contains 1 item this item is already selected so if you select it again the value doesn't change so no change event is fired. Add a placeholder like "Please select item" or something which is selected by default.

Upvotes: 1

Related Questions