lundi 30 mars 2015

disable all value of a multiple select when one is selected, then set a null value to the following field

I have a dynamic form using Ajax and javascript on my Symfony project.


I have a multiple select. I would like in this multiple select when I choose the value 7 which is "outside", all other value of this multiple select are disabled and unselectabled.


Here's the code for the scripts:



function checkOption(obj) {
var select = document.getElementById("mySpace_databasebundle_zonestechnique_batiments");
select.disabled = obj.value == "7";
}


And this is the code for the twig view. Just know that I'am using a collection, i-e a formType in my Symfony project for make this form:



<div>
{{ form_label(form.categorieszonestechnique, "Categorie(s) de la zone technique:", {'label_attr': {'class': 'control-label'}}) }}
{{ form_errors(form.categorieszonestechnique) }}
{{ form_widget(form.categorieszonestechnique, {'attr': {'class': 'selectpicker categories', 'onChange': 'checkOption(this)'}}) }}
</div>

<div>
{{ form_label(form.batiments, "Appartenant au bâtiment:", {'label_attr': {'class': 'control-label'}}) }}
{{ form_errors(form.batiments) }}
{{ form_widget(form.batiments, {'attr': {'class': 'form-control', 'option selected': '--choose abâtiment--'}}) }}
</div>


So when I selected the option 7, i-e "outside", I would like to disable all other value of the multiple select (selectpicker) and make them unselectabled. Then make a default value (null value) to the following field for the select tag of "batiments".


The id for the multiple select (selectpicker) is mySpace_databasebundle_zonestechnique_categorieszonestechnique and the id for the select tag of batiments is mySpace_databasebundle_zonestechnique_batiments. So, the javascript code works like with basic html for creating events, but I didn't find yet the solution to create these events.


Thank you in advance.


Aucun commentaire:

Enregistrer un commentaire