doutian3269 2012-11-13 05:30
浏览 57
已采纳

如果不满足条件,则隐藏附加的表单字段

I have this append script that adds in a set of field on my form. Based on the appended field, I would like for the "upg" select field to ONLY display if the valus for "status" is "Actual". This condition should be applicable to all the appended field set so that if status1 is Actual I have a upg field but is status2 is Ghost, upg2 field should be hidden and so on. What do you guys suggest as the best way to do this?

    <script type="text/javascript">
    var count = 0;
    $(function(){
    $('a#add_field').click(function(){
    count += 1;

    $('#activation').append(
        '<div class="row-fluid">'                   
        +'<div class="span12" style="border-bottom:1px #dddddd; background-color:#e8e8e8; ">'

                +'<div style="float:left; width:7%;">'
                +'<label>&nbsp;</label>'                    
                +'<select name="status' + count + '" id="status' + count + '"  class="input-small">'
                +'<option value="Ghost">Ghost</option>'
                +'<option value="Actual">Actual</option>'
                +'</select>'
                +'</div>'

                +'<div style="float:left; width: 7%">'
                +'<label>Type</label>'
                +'<select id="upg' + count + '" name="upg' + count + '"  class="input-small" >'
                +'<option value="" selected="&nbsp;" >&nbsp;</option>'
                +'<option value="Exp" >Exp</option>'
                +'<option value="Post" >Post</option>'
                +'<option value="Upgrade" >Upg</option>'
                +'<option value="Retail" >Retail</option>'
                +'</select>'
                +'</div>'
         );     
      });   
});
  • 写回答

2条回答 默认 最新

  • doutongxuan1614 2012-11-13 05:51
    关注

    I personally find it easier to break up a complex set of html - especially one that uses some type of logic to determine the final dom - into multiple jquery objects. Although it's slower to append multiple objects together, as opposed to doing it inline, sometimes it's worth the clarity.

    Anyway, you need some data or setting that is determining the initial selected option of status, and you can use that same value to show/hide the corresponding upg. I would suggest you break it up into two appends, hide it before appending it to the dom so you dont see the upg select flash:

    var some_setting_or_db_val = 'ghost';
    
    var row = $('<div class="row-fluid">'                   
        +'<div class="span12" style="border-bottom:1px #dddddd; background-color:#e8e8e8; ">'
    
                +'<div style="float:left; width:7%;">'
                +'<label>&nbsp;</label>'                    
                +'<select name="status' + count + '" id="status' + count + '"  class="input-small">'
                +'<option value="Ghost">Ghost</option>'
                +'<option value="Actual">Actual</option>'
                +'</select>'
                +'</div>'
    
                +'<div style="float:left; width: 7%">'
                +'<label>Type</label>'
                +'<select id="upg' + count + '" name="upg' + count + '"  class="input-small" >'
                +'<option value="" selected="&nbsp;" >&nbsp;</option>'
                +'<option value="Exp" >Exp</option>'
                +'<option value="Post" >Post</option>'
                +'<option value="Upgrade" >Upg</option>'
                +'<option value="Retail" >Retail</option>'
                +'</select>'
                +'</div>');
    
    if(some_setting_or_db_val == 'ghost'){
        row.find("#upg" + count).hide();
    }
    
    $('#activation').append(row)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)