jQuery 启用/禁用显示/隐藏按钮和选择选项。 获取剩余选项值
我有一个使用文本字段中的值填充的选择列表。 我还有两个按钮:一个添加按钮,用于将输入的值添加到选择列表中;一个删除按钮,用于从选择列表中删除输入的值。 我想使用 jQuery 执行以下操作:
- 如果在选择列表中输入文本字段的值不可用,则显示添加按钮并隐藏删除按钮。
- 如果在文本字段中输入的值在选择列表中可用,则隐藏添加按钮并显示删除按钮。
- 如果选择列表为空,则显示添加按钮并隐藏删除按钮。
这是我想出的一些代码:
// 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 中没有值时仍然没有隐藏按钮。 也尝试使用该选项。
I have a select list which is being populated using the values from a text field. I also have two buttons: an add button which adds the entered value to the select list and a remove button which removes the entered value from the select list. I would like to do the following using jQuery:
- If the value entered into the text field is NOT AVAILABLE in the select list, show the add button and hide the remove button.
- If the value entered into the text field is AVAILABLE in the select list, hide the add button and show the remove button.
- If the select list is EMPTY show the add button and hide the remove button.
Here is some code I've come up with:
// 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();
});
It shows the remove button when I add the first value, but if I remove the value the button doesn't show back up.
UPDATE:
Okay I have edited it to this.
$('#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();
}
});
still not hiding the button when no value in the SELECT. Tried it with the option as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信您可以检查选项长度是否>0,表示它有值,如果不是,则它不存在,意味着它没有值,如下所示:
I believe you can check to see if the option length is >0 saying that it has values and if not then it doesn't exist meaning it doesn't have values like so:
如果我正确地理解了这个问题,我认为您想要将其更改
为
:以便将事件处理程序添加到所有选择的选项中,而不仅仅是代码执行时当前选择的选项。 并且还要确保您也为任何新创建的项目设置此处理程序。
If I understand the problem correctly, I think you want to change this:
to this:
so that the event handler gets added to all the select's options, not just the currently selected one when the code executes. And also make sure you're setting up this handler for any newly-created items, as well.
我对原始问题进行了大量编辑,假设我的编辑是正确的,这里是您问题的解决方案:
XHTML:
JavaScript(假设上述文档结构):
I made a heavy edit of the original question, assuming my edit is correct, here is a solution to your problem:
XHTML:
JavaScript (assumes above document structure):
为了获得最佳答案,请尝试以下代码:
使用 Jquery 添加选项来选择标签
http:// /www.formget.com/jquery-add-option-to-select/
For best answer, Try below code :
Add Options to Select Tag Using Jquery
http://www.formget.com/jquery-add-option-to-select/