dpw70180 2013-08-22 19:17
浏览 95

Javascript:如果添加选择,请不要再次添加该选择(不重复)

I currently have many languages for an Airports project I'm working on.

-Currently you can select whatever language you would like from the drop-down menu and it will appear in the list. Working fine. Can delete them too.

-The goal: once a language has been added to the list, you should not be able to add it again. For example, you can hit the 'Add Languages' for French and add it as many times as you want. This goes for any language.

The current js code for adding a language:

function addLanguage()
{
        var languages = $("#languages_dd").val();
        language_display = languages.split("-");
        alert(languages);

        var units = $("#units_dd").val();
        var unit_display = $("#units_dd :selected").text();

        $(".none_class").hide();
        $("#error_msg").html("");
        $("#summary").append("<li><input type='radio' name='language_item'> <span class='route_summary_field_big'>"+language_display[0]+"</span>"+unit_display+"<input type='hidden' name='languages[]' value='"+languages+"'><input type='hidden' name='units[]' value='"+units+"'></li>");
}

I'm not too familiar with javascript and have been searching around online. I know it's going to be a conditional, something along the lines of:

if($("#languages_dd :selected")
{
//do something;
}
else if
//do something else;
}

Any input is appreciated to steer me in the right direction.

  • 写回答

2条回答 默认 最新

  • doufan6886 2013-08-22 19:24
    关注

    You can keep the selected languages in an array. Then before you append to the html, check whether the value already exists:

    var selected_lang = new Array();
    function addLanguage() {
    //your code here..
        if (!$.inArray(language_display, selected_lang)) {
            selected_lang.push(language_display)
            // rest of your code to append html
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测