繁体   English   中英

我想隐藏字段,如果其值为空,并且使用Javascript在HTML中也使用image src

[英]I want to hide Fields if its value is empty and image src also in HTML using Javascript

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

<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() ,如果value为空,则隐藏输入。

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

用于filter()的API文档

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

$(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();
        }
     });
  });

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM