gpt4 book ai didi

javascript - 我想删除重复添加

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:24 25 4
gpt4 key购买 nike

我正在研究 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com