简体   繁体   English

如何使用 jQuery 隐藏所选选项的某些文本值

[英]How to Hide the Certain Text Value of Selected Option using jQuery

I have a select box that contains certain elements (see below):我有一个包含某些元素的选择框(见下文):

    <select id="test" name="test">
         <option value="12345">12345 - Test</option>
         <option value="67890">67890 - Test 2</option>
         <option value="53453">53453 - Test 3</option>
    </select>

Here is what I'm trying to accomplish When the user selects a certain element, I want certain elements of the text to hide.这是我要完成的任务 当用户选择某个元素时,我希望隐藏文本的某些元素。 For example, if I select 12345, I want the - Test to hide.例如,如果我选择 12345,我希望 - 测试隐藏。

How can I do it in jQuery?我怎样才能在 jQuery 中做到这一点?

Thank you, Kevin谢谢你,凯文

A brute force-y way to do it would be to keep track of the internal text and values of the currently-selected option, then replace the text with the desired text when the option changes.一种蛮力的方法是跟踪当前所选选项的内部文本和值,然后在选项更改时将文本替换为所需的文本。 This part's easy in your case, since the desired output text is the same as the option's value.这部分在您的情况下很容易,因为所需的输出文本与选项的值相同。

I opted to add a blank option at the beginning, since by default the text will not have changed, but you can get around this by adding a trigger to go off when the page is finished loading.我选择在开始时添加一个空白选项,因为默认情况下文本不会改变,但是您可以通过添加一个触发器来解决这个问题,当页面加载完成时关闭。

 var selectedTextOrig = ""; var selectedValue = 0; $("#test").change(function() { // Reset the inner html text for the previously-selected option $("#test option").each(function() { if ($(this).val() == selectedValue) { $(this).text(selectedTextOrig); return false; // break out of $.each() } }); // Store the viewable text and value of the currently selected selectedValue = $("#test option:selected").val(); selectedTextOrig = $("#test option:selected").text(); // Replace the current inner html $("#test option:selected").text(selectedValue); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="test" name="test"> <option disabled="disabled" selected></option> <option value="12345">12345 - Test</option> <option value="67890">67890 - Test 2</option> <option value="53453">53453 - Test 3</option> </select>

I have found a solution:我找到了一个解决方案:

Here is how I achieved it:这是我实现它的方法:

        $('select[name="test"]').find('option').remove().end();
        $('select[name="test"]').append(new Option(selectId, selectId));
        $('select[name="test"]').trigger('change');

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

相关问题 如何使用jquery显示选定的选项值? - How to show the selected option value using jquery? 使用jquery或javascript在选择框中隐藏重复的选项文本值 - Hide repeated option text value in select box using jquery or javascript 当从下拉菜单中选择选项时,如何使用jQuery将所选选项隐藏到另一个下拉菜单中 - When selected option from dropdown then how to hide that selected option into another dropdown using jQuery 在jQuery中使用此关键字获取所选选项的文本值 - Get the text value of selected option using this keyword in jquery 使用jQuery获取所选下拉菜单选项的文本值 - get text value of selected dropdown menu option using jquery 如何获取选择jquery对象的所选选项的文本值? - How to get the text value of the selected option of a select jquery object? 如何隐藏\\显示基于JQuery的另一个选定选项的选项的特定值? - How to hide\show specific value of option based on another selected option by JQuery? 如何使用jquery获取选定选项的文本? - How to get the text of the selected option of a select using jquery? 如何使用jquery或javascript使用选定的下拉选项文本设置innerhtml? - How to set innerhtml with selected dropdown option text using jquery or javascript? 如何使用 Javascript 或 Jquery 获取所选选项的值? - How can i get the value of the selected option using Javascript or Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM