简体   繁体   English

我将如何防止价值重复和空值

[英]How will i prevent duplication of value and empty value

How can I prevent duplicate values being added to a combobox? 如何防止重复值添加到组合框? I also need to prevent the space value. 我还需要防止空格值。 This is my code but its not working. 这是我的代码,但不起作用。

An entry is entered the first time input but the second time I enter input its alerting me that I have entered a duplicate value even when I enter different values. 第一次输入时输入一个条目,但是第二次输入时输入该条目,提醒我即使输入了不同的值,我也输入了重复的值。

Please see this jsFiddle https://jsfiddle.net/adLxoakv/ 请参阅此jsFiddle https://jsfiddle.net/adLxoakv/

<HTML>
        <HEAD>
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
            <fieldset>
                <legend>Combo box</legend>
                Add to Combo: <input type="text" name="txtCombo" id="txtCombo"/>
                            Selected: <input type="text" name="selected" id="selected"/>
                            IMEI Selected: <input type="text" name="imei" id="imei"/>

                <input type="button" id="button" value="Add" onclick="addCombo()">
                <br/>
                Combobox: <select name="combo" multiple id="combo"></select>
            </fieldset>
        </BODY>
    </HTML>
    <script>

    $("#txtCombo").on("keydown", function (e) {
        return e.which !== 32;
    });


    $(document).ready(function() {

    $('#button').click(function(){

        var data = [];
        $.each($("#combo option:selected"), function() {
          data.push($(this).attr("value"));

        });
            $('#imei').val(data.join(","));;
    var count = $("#combo :selected").length;
    $('#selected').val(count);


      });

    });


        $("#combo").on('change',  function () {

    var count = $("#combo :selected").length;
    $('#selected').val(count);


    });
         var text = $("#text").val();


     function addCombo() {

            var textb = document.getElementById("txtCombo");
            var combo = document.getElementById("combo");
            var option = document.createElement("option");
            option.text = textb.value;
            option.value = textb.value;
            option.selected = true;
        if (textb.length == 0) {
          return false;
        }
           if (combo.length) {
          alert("Duplicate found");
          return false;
        }
            try {
                combo.add(option, null ); //Standard 
            }catch(error) {
                combo.add(option); // IE only
            }
            textb.value = "";
        }


        // separated by comma to textbox
        $(document).ready(function() {

      $("#combo").change(function() {
        var data = [];
        $.each($("#combo option:selected"), function() {
          data.push($(this).attr("value"));
        });
            $('#imei').val(data.join(","));;

      });

    });
    </script>

To find the duplicate you can use following function(using jQuery) 要查找重复项,可以使用以下函数(使用jQuery)

function isDuplicate(value,text){
/*Get text of the option identified by given value form the combobox and then check if its text matches the given text*/
    if($('#combo select option[value="' + value + '"]').text() == text)
        return true;
    else 
        return false;
}

Update: 更新:

function addCombo() {

    var textb = document.getElementById("txtCombo");
    var combo = document.getElementById("combo");
    var option = document.createElement("option");
    var value = textb.value.trim();
    option.text = value;
    option.value = value;
    option.selected = true;
    if (textb.length == 0) {
        return false;
    }
    if ($('#combo option[value="' + value + '"]').text() == value ) {
        alert("Duplicate found");
        return false;
    }
    try {
        combo.add(option, null ); //Standard 
    }catch(error) {
        combo.add(option); // IE only
    }
    textb.value = "";
}

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

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