使用jQuery在第一个下拉菜单中选择后,启用第二个下拉菜单中的数据。

huangapple go评论64阅读模式
英文:

Enabling the data from second dropdown when it selected in the first dropdown with jQuery

问题

<select class="form-control" id="select2">
   <option value="1556456">Shiva</option>
   <option value="34234">Disha</option>
</select>

<select class="form-control" id="select3">
    <option value="1556456">Shiva</option>
    <option value="34234">Disha</option>
</select>
英文:

How can I write a jQuery function that enables the value from second dropdown only if a value is selected on the first dropdown?

As I selected Shiva in first drop down it should be disabled from 2nd drop down.

fist drop down value must not be equal to 2nd dropdown.

<select class="form-control" id="select2">
   <option value="1556456">Shiva</option>
   <option value="34234">Disha</option>
</select>

<select class="form-control" id="select3">
    <option value="1556456">Shiva</option>
    <option value="34234">Disha</option>
</select>

答案1

得分: 0

这部分内容的翻译如下:

"不确定这是否是您的意思。请告诉我。您可以在其上使用.find和.filter。

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->

$('select').on('change', function() {
    $('option').prop('disabled', false);
    $('select').each(function() {
        var val = this.value;
        $('select').not(this).find('option').filter(function() {
            return this.value === val;
        }).prop('disabled', true);
    });
}).change();

<!-- language: lang-html -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<select class="form-control" id="select2">
   <option value="1556456">Shiva</option>
   <option value="34234">Disha</option>
</select>

<select class="form-control" id="select2">
    <option value="1556456">Shiva</option>
    <option value="34234">Disha</option>
</select>

</form>

<!-- end snippet -->
```"

<details>
<summary>英文:</summary>

Not sure if this is what you mean. Let me know. You can use a .find and .filter on it.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    $(&#39;select&#39;).on(&#39;change&#39;, function() {
        $(&#39;option&#39;).prop(&#39;disabled&#39;, false);
        $(&#39;select&#39;).each(function() {
            var val = this.value;
            $(&#39;select&#39;).not(this).find(&#39;option&#39;).filter(function() {
                return this.value === val;
            }).prop(&#39;disabled&#39;, true);
        });
    }).change();

&lt;!-- language: lang-html --&gt;

    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;form&gt;
    &lt;select class=&quot;form-control&quot; id=&quot;select2&quot;&gt;
       &lt;option value=&quot;1556456&quot;&gt;Shiva&lt;/option&gt;
       &lt;option value=&quot;34234&quot;&gt;Disha&lt;/option&gt;
    &lt;/select&gt;

    &lt;select class=&quot;form-control&quot; id=&quot;select2&quot;&gt;
        &lt;option value=&quot;1556456&quot;&gt;Shiva&lt;/option&gt;
        &lt;option value=&quot;34234&quot;&gt;Disha&lt;/option&gt;
    &lt;/select&gt;

    &lt;/form&gt;

&lt;!-- end snippet --&gt;



</details>



huangapple
  • 本文由 发表于 2023年2月6日 14:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定