简体   繁体   English

当使用Jquery / Javascript选择一个时,如何使所有选择中的所有值都无法选择:

[英]How to make all values unselectabled of a multiple select when one is selected with Jquery/Javascript:

I have a dynamic form using Ajax and jQuery on my Symfony project. 我在Symfony项目上使用Ajax和jQuery生成动态表单

I have a multiple select . 我有多项选择 I would like in this multiple select, when I choose the value id 7 (which is "outside") , all other value are unselectabled. 我想在这种多重选择中,当我选择值id 7(在“外部”)时 ,所有其他值都是不可选择的。

Here's the code for the script in my twig view: 这是我的树枝视图中脚本的代码:

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

And this is the form code in my twig view. 这是我的树枝视图中的表单代码。 Just know that I'am using a collection, ie a formType of Symfony Form to make this form: 只知道我正在使用一个集合,即Symfony Form的formType来创建此表单:

<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, ie "outside", I would like to make all other value of the multiple select unselectabled. 因此,当我选择选项7(即“外部”)时,我想使多重选择的所有其他值变为不可选择。 Then make a default value (null value) to the following field for the select tag of "batiments". 然后为“ 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 . 对于多选(selectpicker)idmySpace_databasebundle_zonestechnique_categorieszonestechniquebatiments的选择标签IDmySpace_databasebundle_zonestechnique_batiments

Someone know how to make all other values of my multiple select unselectable, then put a null value to the following fields, ie the select tag for batiments. 有人知道如何使我的多重选择的所有其他值变为不可选择,然后将空值添加到以下字段,即用于饼的选择标记。

Thank you. 谢谢。

  function lock() { $("input").prop('disabled', true); $("select").prop('disabled', true); }; function unlock() { $("input").prop('disabled', false); $("select").prop('disabled', false); }; 
 <form> <input type="checkbox" id="pizza" name="pizza" value="yes"><br/> <input type="radio" name="radio" id="radio" value="Yes"> <input type="radio" name="radio" id="radio" value="No"><br/> <select id="pizza_kind"> <option>(choose one)</option> <option value="margaritha">Margaritha</option> <option value="hawai">Hawai</option> </select><br/> <select id="pizza_kind1"> <option>(choose one)</option> <option value="margaritha">Margaritha</option> <option value="hawai">Hawai</option> </select><br/> <button name="freez" onClick="lock()">Lock</button> <button name="freez" onClick="unlock()">Unlock</button> </form> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 

I think this is what I understand from your explanation. 我认为这就是您的解释。 If its not what you want then let me know 如果不是您想要的,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM