我有一个使用文本字段中的值填充的选择列表。我还有两个按钮:一个添加按钮,它将输入的值添加到选择列表中,一个删除按钮,它从选择列表中删除输入的值。我想使用 jQuery 执行以下操作:
- 如果输入文本字段的值在选择列表中不可用,则显示添加按钮并隐藏删除按钮。
- 如果输入文本字段的值在选择列表中为AVAILABLE,则隐藏添加按钮并显示删除按钮。
- 如果选择列表为空,则显示添加按钮并隐藏删除按钮。
这是我想出的一些代码:
// Remove selected options
$('#removeButton').click(function() {
//$.map($('#addedchargeamtid :selected'), function(e) {
$.map($('#addedchargeamtid option'), function(e) {
var exp = $(e).text();
// Would like to have all the Option values in CVS format 0.00,1.99, etc...
// If removed this should also remove the value in the array
})
$('#removeButton').hide();
return !$('#addedchargeamtid :selected').remove();
});
// Add charge amount
$('#addedchargeamtid option:selected').focus(function() {
$('#removeButton').show();
});
当我添加第一个值时,它会显示删除按钮,但如果我删除该值,则按钮不会显示备份。
更新:
好的,我已将其编辑为此。
$('#removeButton').click(function() {
$('#addedchargeamtid :selected').remove();
$.map($('#addedchargeamtid option'), function(e) {
var exp = $(e).text();
alert(exp); // Need this in CSV format but I think it displays the correct values
})
//if($("#addedchargeamtid option").length > 0) { <-- Didn't work
if($("#addedchargeamtid").length > 0) {
$('#removeButton').show();
} else {
$('#removeButton').hide();
}
});
当 SELECT 中没有值时,仍然没有隐藏按钮。也尝试了该选项。