我在PHP代码中研究这个概念。同时选择下拉框。我追加了文本框。我想删除重复的值,反之亦然

$package=array("Sliver","Gold","Broze","Limited","Unlimited","Constants");
<select id="choose_package" >
<?php foreach ($package as $packagevalue) {?>
    <option value="<?php echo $packagevalue;?>"> <?php echo $packagevalue;?></option>
<?php } ?>
</select>
<div class="package_limit"></div>
$(function () {
    $("#choose_package").on("change", function () {
        var selectval=$("#choose_package").val();
        var div = $("<div />");
        div.html(GetDynamicTextBox(selectval));
        $(".package_limit").append(div);
    });
    $("body").on("click", ".remove", function () {
        $(this).closest("div").remove();
    });
});

function GetDynamicTextBox(value) {
    return '<input name = "package_name_'+ value +'" readonly type="text" value = "' + value + '" />&nbsp;' +
    '<input name = "package_price_'+ value +'" type="text" value = "" />&nbsp;'+
    '<input type="button" value="Remove" class="remove" />'
}

https://jsfiddle.net/JVRK/w7xfzdus/13/

最佳答案

试试这个:

https://jsfiddle.net/mvwns78h/

var arr = new Array();
$("#choose_package").on("change", function () {
    var selectval=$("#choose_package").val();
    if($.inArray(selectval, arr) == -1) { // check if selected value is not in array then only append
       var div = $("<div />");
       div.html(GetDynamicTextBox(selectval));
       $(".package_limit").append(div);
       arr.push(selectval); // push selected value in array
    }
});

关于javascript - 我要删除重复添加的内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38204526/

10-11 05:29