【发布时间】:2009-05-18 21:08:15
【问题描述】:
我有一个使用文本字段中的值填充的选择列表。我还有两个按钮:一个添加按钮,它将输入的值添加到选择列表中,一个删除按钮,它从选择列表中删除输入的值。我想使用 jQuery 执行以下操作:
- 如果在选择列表中输入文本字段的值是NOT AVAILABLE,则显示添加按钮并隐藏删除按钮。
- 如果在选择列表中输入文本字段的值是AVAILABLE,则隐藏添加按钮并显示删除按钮。
- 如果选择列表是EMPTY,则显示添加按钮并隐藏删除按钮。
这是我想出的一些代码:
// 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 中没有值时仍然不隐藏按钮。也尝试了该选项。
【问题讨论】:
-
你是什么意思按钮不显示备份?in last sentence
-
据我了解,如果值存在则显示按钮,如果值不存在则隐藏按钮?
-
更正这是我要找的:)
-
“addchargeamtid”是您选择标签的 id 名称吗?
-
yes addedchargeamtid 是 id
标签: javascript jquery dhtml