jQuery将小数添加到SELECT,没有重复选项删除
Posted
技术标签:
【中文标题】jQuery将小数添加到SELECT,没有重复选项删除【英文标题】:jQuery add decimal to SELECT, No Duplicates with option to remove 【发布时间】:2010-10-27 02:29:13 【问题描述】:到这里就OK了:需要过滤掉重复项并能够删除添加到 SELECT 框中的值。
Number: <input type="text" name="number" />
<br />
<!-- Clicking the button should add an option w/ the value being the text displayed -->
<button type="button">Add</button>
<br />
<Select id="mySelect" size="9"></Select>
<br />
<!-- Display only if SELECT box has a value -->
<button type="button" hidden="hidden">Remove</button>
我查看了this thread,但仍然缺少一些东西。
【问题讨论】:
这里的问题我没看懂? 【参考方案1】:我不太确定你在问什么,所以澄清一下会很有帮助。这是我对您正在寻找的东西的破解:
Javascript
<script type="text/javascript" charset="utf-8">
$( function()
// Add number from <input> as an <option> to the <select>
$('#add_number').click( function()
// Get Number from <input>
var numberToAdd = $('#number_to_add').val();
// Make sure it's not a duplicate; if so, don't add
var match = false;
$('#mySelect option').each( function()
if (numberToAdd == this.value) match = true;
);
if (match) return false;
// Add the number to the <select>
$('#mySelect').append(
$('<option></option>').html(numberToAdd).val(numberToAdd)
);
// Show the remove button
$('#remove_selected').show();
return false;
);
// Remove the currently selected <option> in the <select>
$('#remove_selected').click( function()
$('#mySelect option:selected').remove();
return false;
);
)
</script>
HTML
<form action="#">
<p>
Number: <input type="text" name="number_to_add" id="number_to_add" />
<button id="add_number" type="button">Add</button>
</p>
<p>
<select id="mySelect" size="9"></select>
<button id="remove_selected" type="button" style="display:none;">Remove</button>
</p>
</form>
【讨论】:
以上是关于jQuery将小数添加到SELECT,没有重复选项删除的主要内容,如果未能解决你的问题,请参考以下文章