我想隐藏字段,如果其值为空,并且也在HTML中使用Javascript生成图像src [英] I want to hide Fields if its value is empty and image src also in HTML using Javascript

查看:85
本文介绍了我想隐藏字段,如果其值为空,并且也在HTML中使用Javascript生成图像src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果字段为空并且图像src也为html中如何隐藏字段.使用Java脚本.请建议

how to hide fields in html if fields are empty and the image src also. using java script. please suggest

<div id="pagewrapper" >
    <div class="row" >
        <div class="column-half" style="width:500px">
            <div class="containerdiv" >

                <form:label path="file4Desc" type="text" value="" maxlength="50">
                </form:label>
                <form:input path="file4Desc" value="${agreement.file4Desc}" 
                    style="width:300px"/>
                <font color="red"><form:errors path="file4Desc" /></font>           
            </div>
        </div>
        <div class="column-half" style="width:13%">         
            <div class="containerdiv">
                <form:label path="filename3" type="text" value="" maxlength="50">
                </form:label>
                <a href="${pageContext.request.contextPath}/customer/viewDownload3/${agreement.agreementId}.htm">
                    <img src="${pageContext.request.contextPath}/resources/images/download3.gif" 
                                border="0"
                                title="Download this document"/>
                </a>

            </div>
        </div>
    </div>
</div>

推荐答案

尝试一下:您可以使用.filter()并在值为空时隐藏输入.

Try this : You can make use of .filter() and hide input if value is empty.

$(function(){
 $('.containerdiv input').filter(function(){
   return $(this).val().trim()=='';
 }).hide();
});

用于filter()的API文档

API Document for filter()

编辑-由于OP要同时隐藏输入和图像(如果输入值为空),请参见下面更新的解决方案

EDIT - As OP want to hide both input and image if input value is empty, please see below updated solution

$(function(){
     $('.containerdiv input').each(function(){
       if($(this).val().trim()=='')
       {
          //hide parent div and next div having image
          var $parentDiv = $(this).closest('.column-half');
          $parentDiv.next('.column-half').hide();
          $parentDiv.hide();
        }
     });
  });

这篇关于我想隐藏字段,如果其值为空,并且也在HTML中使用Javascript生成图像src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆