使用jQuery在输入字段中输入相同数据时禁用下拉数据。

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

disabling the data from dropdown when same data we typed in input field with jquery

问题

你可以编写一个 jQuery 函数,以便仅当输入字段中的文本与下拉菜单中的选项匹配时,禁用该选项。

$(document).ready(function() {
    $('#select2').change(function() {
        var inputText = $('#newInputField').val(); // 获取输入字段的值
        var selectedOption = $('#select2 option:selected').text(); // 获取所选选项的文本

        if (inputText === selectedOption) {
            $('#select2 option:selected').prop('disabled', true); // 如果匹配,禁用选项
        } else {
            $('#select2 option').prop('disabled', false); // 否则启用所有选项
        }
    });
});

请注意,你需要将 #newInputField 替换为实际的输入字段的 ID。这个函数会在输入字段的值发生变化时检查匹配并相应地禁用或启用下拉菜单中的选项。

英文:

How can I write a jQuery function that disables the value from dropdown only if same data we typed in input field.

Input Field - NEW

So from drop down new should be disabled.

<select class="form-control" id="select2">
   <option value="1">New</option>
   <option value="2">OLD</option>
</select>

答案1

得分: 0

你可以这样做:

$('.typeVal').on("keyup change",function() {
  var $val = $(this).val();
  $('#select2 option').prop("disabled", function() {
    return $(this).text() == $val;
  });
});

这段代码使用jQuery将事件处理程序绑定到具有类名.typeVal的元素上。事件处理程序将在这些元素上的"keyup"和"change"事件上触发。

当事件触发时,代码获取触发事件的元素的值,并将其存储在变量$val中。然后,它根据一个函数设置#select2元素内所有

如果

英文:

You can do it like this:

$('.typeVal').on("keyup change",function() {
  var $val = $(this).val();
  $('#select2 option').prop("disabled", function() {
    return $(this).text() == $val;
  });
});

This code is using jQuery to bind an event handler to elements with the class .typeVal. The event handler will be triggered on "keyup" and "change" events on these elements.

When the event is triggered, the code gets the value of the element that triggered the event and stores it in a variable $val. Then, it sets the "disabled" property of all the <option> elements within the #select2 element based on a function.

The function returns true if the text content of the <option> element is equal to $val, meaning it will disable the <option> if it has the same text content as the triggering element. If the text content of the <option> is not equal to $val, the function returns false and the <option> will not be disabled.

Demo

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

<!-- language: lang-js -->

$(&#39;.typeVal&#39;).on(&quot;keyup change&quot;,function() {
  var $val = $(this).val();
  $(&#39;#select2 option&#39;).prop(&quot;disabled&quot;, function() {
    return $(this).text() == $val;
  });
});

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input class=&quot;typeVal&quot; /&gt;

&lt;select class=&quot;form-control&quot; id=&quot;select2&quot;&gt;
  &lt;option value=&quot;1&quot;&gt;New&lt;/option&gt;
  &lt;option value=&quot;2&quot;&gt;OLD&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定