简体   繁体   English

替换选择框选项中的某些文本

[英]Replace some of the text in select box option

I have this Show/Hide select box ( JS Fiddle ) that gets a classname from a selected option and hides a corresponding unordered list item. 我有一个“显示/隐藏”选择框JS Fiddle ,它从选定的选项中获取类名,并隐藏相应的无序列表项。 I am at a loss to use replace() to replace the text Hide with Show or vice versa when an option is clicked.(eg Show Month should become Hide Month on click.) 当单击一个选项时,我不知所措要使用replace()将文本Hide替换为Show ,反之亦然(例如,单击时Show Month应该变成Hide Month。)

I also want to change all the options back to Hide when I click the Show All option. 当我单击全部显示选项时,我还想将所有选项都更改回Hide

When I used findoption.replace('Hide','Show'); 当我使用findoption.replace('Hide','Show'); I got this Uncaught TypeError: undefined is not a function error. 我收到了这个Uncaught TypeError: undefined is not a function错误。 Can anyone show me how to do that? 谁能告诉我该怎么做? Any help would be appreciated. 任何帮助,将不胜感激。

JS Code: JS代码:

$(document).ready(function(){
    $(".showhidelist").change(function() {
        $this = $(this);
        var findoption = $this.find('option:selected');
        var selected = findoption.data('hide');
        var show_hide_li = $("."+selected);
        if (show_hide_li.css("display") == "none") {
            show_hide_li.show(); 
           /* Want to Replace the text Hide with Show */
        }
        else if (show_hide_li.is(':visible')){
            show_hide_li.hide();
          /* Want to Replace the text Show with Hide */
        }
        else if (selected == "Reset") {
            $('li').show();
         /* Want to Replace the text Show with Hide if it's true */
        }
    });

});

HTML HTML

<select class="showhidelist">
    <option data-hide="Reset">Show All</option>
    <option data-hide="year">Hide Year</option>
    <option data-hide="month">Hide Month</option>
    <option data-hide="day">Hide Day</option>
</select>

<ul id="list">
    <li class="year">2004</li>
    <li class="month">Feb</li>
    <li class="day">17</li>

<ul>

You are not getting the text() into the replace, and you could use the function that .text() has to replace the text. 您没有将text()替换,而是可以使用.text()必须替换的函数。

findoption.text(function(_,text){ return text.replace('Hide', 'Show')});

Change the word 'Hide' or 'Show' depend on what you want. 根据您的需要更改单词“隐藏”或“显示”。

Update: 更新:

I read in caspian comment, that you problem now is that you could not re-click an option clicked, try restarting the select to first option like: 我在里海评论中读到,您现在遇到的问题是您无法重新单击所单击的选项,请尝试重新启动“选择第一个选项”,例如:

$(".showhidelist").prop('selectedIndex',0);

findoption is returns a jquery array, so you would need to access the first element there. findoption返回一个jquery数组,因此您需要访问那里的第一个元素。 I think the following will achieve what you're looking for (as far as the selected element goes). 我认为以下内容将满足您的需求(就所选元素而言)。

findoption[0].innerHTML = findoption[0].innerHTML.replace('Show','Hide');

I'm not sure what you're really doing - this gets a little quirky, since you have the show/hide all and you'll have to go through the whole list to update them when you do. 我不确定您实际上在做什么-这有点古怪,因为您拥有全部的显示/隐藏功能,因此您必须浏览整个列表才能更新它们。 Also - you aren't getting a selected event when you re-select what's already selected. 另外-重新选择已选择的事件时,不会得到选定的事件。

UPDATE: this includes how to update all items in the list using each() UPDATE:这包括如何使用each()更新列表中的所有项目

jsfiddle: http://jsfiddle.net/hk4wg/15/ jsfiddle: http : //jsfiddle.net/hk4wg/15/

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

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