2

我有一个使用文本字段中的值填充的选择列表。我还有两个按钮:一个添加按钮,它将输入的值添加到选择列表中,一个删除按钮,它从选择列表中删除输入的值。我想使用 jQuery 执行以下操作:

  1. 如果输入文本字段的值在选择列表中不可用,则显示添加按钮并隐藏删除按钮。
  2. 如果输入文本字段的值在选择列表中为AVAILABLE,则隐藏添加按钮并显示删除按钮。
  3. 如果选择列表为,则显示添加按钮并隐藏删除按钮。

这是我想出的一些代码:

// 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 中没有值时,仍然没有隐藏按钮。也尝试了该选项。

4

4 回答 4

2

我相信您可以检查选项长度是否 >0 表示它具有值,如果不是,则它不存在意味着它没有像这样的值:

if($("#addedchargeamtid option").length > 0 ) //if addedchargeamtid is the id of select tag
{
   $('#removeButton').show();
}
else
{
  $('#removeButton').hide();
}
于 2009-05-18T21:21:18.863 回答
0

如果我正确理解了这个问题,我想你想改变这个:

// Add charge amount
$('#addedchargeamtid option:selected').focus(function() {
   $('#removeButton').show();
});

对此:

// Add charge amount
$('#addedchargeamtid option').focus(function() {
   $('#removeButton').show();
});

这样事件处理程序就会被添加到所有选择的选项中,而不仅仅是代码执行时当前选择的选项。还要确保为任何新创建的项目设置此处理程序。

于 2009-05-18T21:24:39.530 回答
0

我对原始问题进行了大量编辑,假设我的编辑是正确的,这里是您问题的解决方案:

XHTML:

<input type="text" id="textField" />
<input type="button" id="addButton" value="Add" />
<input type="button" id="removeButton" value="Remove" />
<select multiple="multiple" id="selectList">
</select>

JavaScript(假设上面的文档结构):

$(function(){

  $("#textField").keypress(function(){

    var val = $(this).val();

    if (val === '') {
      return;
    }

    // Clear selections
    $("#selectList option").removeAttr("selected");

    // Value not in select list
    if ($("#selectList option").length === 0 || $("#selectList option[value="+val+"]").length === 0) {
      $("#removeButton").hide();
      $("#addButton").show();

    // Value in select list
    } else {
      $("#removeButton").show();
      $("#addButton").hide();

      // Select it
      $("#selectList option[value="+val+"]").attr("selected", "selected");
    }
  });

  $("#removeButton").click(function(){
    var val = $("#textField").val();
    val !== '' && $("selectList option[value="+val+"]").remove();
  });

  $("#addButton").click(function(){
    var val = $("#textField").val();
    val !== '' && $("#selectList").append('<option value="'+val+'" label="'+val+'">'+val+'</option>');
  });

});
于 2009-05-18T22:05:21.303 回答
-1

为了获得最佳答案,请尝试以下代码:

添加选项以使用 Jquery 选择标签

$(document).ready(function(){

//Function To Add or Remove New Option Field in Form
var i=2;
$(".add").on("click", function add_new(){
$(".more").append($("<p/>").append(
 "<input type='text' id='option"+i+"' placeholder='option"+i+"'/>",
 $("<img/>",{class:'add', src:'images/add.png'}).click(function(){add_new();}),
 $("<img/>",{class:'del', src:'images/del.png'}).click(function(){$(this).parent().remove();})
 ))
i=i+1;
});

//Below Function Executes on Click of create select Button
$("#button").on("click",function(){

//To Clear Previous Select Option Field if Exists in Div
$("#prm").empty();

//Creating Select Option in Div
$("#prm").append("<select></select>");

//Creating Options and Adding it To Above Select Tag
 $("input[type=text]").each(function(){
 if($(this).val()==""){
 alert($(this).attr('id')+" is Empty !");
 }
 else{
 $("select").append('<option>'+$(this).val()+'</option>');
 }
 });

});

});

http://www.formget.com/jquery-add-option-to-select/

于 2014-07-15T06:23:12.447 回答