我有一个使用文本字段中的值填充的选择列表。我还有两个按钮:一个添加按钮,用于将输入的值添加到选择列表中;一个删除按钮,用于从选择列表中删除输入的值。我想使用 jQuery 执行以下操作:
- 如果在文本字段中输入的值为无法使用在选择列表中,显示添加按钮并隐藏删除按钮。
- 如果在文本字段中输入的值为可用的在选择列表中,隐藏添加按钮并显示删除按钮。
- 如果选择列表是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();
});
当我添加第一个值时,它会显示删除按钮,但如果我删除该值,该按钮不会显示出来。
UPDATE:
好的,我已经将其编辑为这个。
$('#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 中没有值时仍然没有隐藏按钮。也尝试使用该选项。
我相信您可以检查选项长度是否>0,表示它有值,如果不是,则它不存在,意味着它没有值,如下所示:
if($("#addedchargeamtid option").length > 0 ) //if addedchargeamtid is the id of select tag
{
$('#removeButton').show();
}
else
{
$('#removeButton').hide();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)