【问题标题】:jQuery enable/disable show/hide button w/ SELECT options. Get remaining option valuesjQuery 启用/禁用带有 SELECT 选项的显示/隐藏按钮。获取剩余的选项值
【发布时间】:2009-05-18 21:08:15
【问题描述】:

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

  1. 如果在选择列表中输入文本字段的值是NOT AVAILABLE,则显示添加按钮并隐藏删除按钮。
  2. 如果在选择列表中输入文本字段的值是AVAILABLE,则隐藏添加按钮并显示删除按钮。
  3. 如果选择列表是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


【解决方案1】:

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

if($("#addedchargeamtid option").length > 0 ) //if addedchargeamtid is the id of select tag
{
   $('#removeButton').show();
}
else
{
  $('#removeButton').hide();
}

【讨论】:

  • 我得到了它的工作我刚刚为添加按钮添加了点击操作:)
  • 还有一个问题,仍然需要遍历所有选项值并将它们以逗号分隔值 (CVS) 格式保存。示例:val1、val2、val3 等...现在使用地图但有更好的方法吗?
【解决方案2】:

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

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

到这里:

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

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

【讨论】:

    【解决方案3】:

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

    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>');
      });
    
    });
    

    【讨论】:

      【解决方案4】:

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

      添加选项以使用 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/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多