elclanrs / jq-idealforms-old

The ultimate framework for building and validating responsive HTML5 forms.
665 stars 95 forks source link

Can't refresh selected option filled dinamically #102

Closed SuperSpe closed 11 years ago

SuperSpe commented 11 years ago

Hi. I have a form with a select/option with a default value selected. I'm using an ajax call wich fill all form's field from a json object. I have only one problem with the select/option. So i Have to change the default option selected with the one that is in the json object. The result is that the original (and not displayed) html is changed with the attribute "selected=selected" but the jq-idealform listbox is not updated. I tried with freshFields(), fresh() and reload() withous results. The select is displaying a non correct value and I can't update it. Thanks

elclanrs commented 11 years ago

Yeah, I'm aware of these problems with the select menu. There are other questions still open with similar issues. It definetely needs some love... For now if you want to make it work with AJAX the best advice would be to either use the default select menu or switch to select2 (use disableCustom: 'select' first).

gine commented 10 years ago

This work for me putting that part of code on fillform function called after the idealform creation and initialization:

select=$(this);
$(select).next().find('span.ideal-select-title').text(title); 
opts=$(select).find('option');
$(opts).each(function(i){
    if($(this).val()===title)
       $(this).prop('selected', true);
});
$(select).trigger('change');

It makes the same things that you can find on select.js but in this mode work. I dont't know why, don't ask to me.

Select2 look great, too. Tnx for the link.

elclanrs commented 10 years ago

I created a new replacement for Ideal Forms 3 but it will be maintained on a different repo, see here https://github.com/elclanrs/jquery.idealselect. You can add dynamic options and refresh the select easily:

$('select').idealselect();
var options = [
  '<option value="one">One</option>',
  '<option value="two">Two</option>',
  '<option value="three">Three</option>'
];
$('select').append(options.join('')).idealselect('_build');