为了账号安全,请及时绑定邮箱和手机立即绑定

如果条件在laravel中,如何检查隐藏输入为空或不使用else

如果条件在laravel中,如何检查隐藏输入为空或不使用else

慕妹3242003 2021-12-02 19:32:25
这是我隐藏的输入字段<input type="hidden" id="hdnship" name="origin_port"  value="">这是我获取隐藏输入字段值的函数function myfunction(){     $('#printarray').on('click', function() {          var array = [];          $("input:checkbox[name=origin_port]:checked").each(function() {              array.push($(this).val());           });          alert(array);         $('#hdnship').val(array);      });  }
查看完整描述

3 回答

?
弑天下

TA贡献1818条经验 获得超7个赞

你也可以试试这个检查是否为hidden空


$(document).ready(function(){

  

  $(".jsButton").click(function(){

       var array = [],emptyvalue=[]; 

         $("input[type=hidden]").each(function() 

        {        

             if($(this).val().length !== 0)

             {

                 array.push($(this).val());                     

             }

             else

             {

               emptyvalue.push("value");

             } 

         });          

         $('#lblHasValue').html(array.join(',')); 

         $('#lblNotValue').html(emptyvalue.join(',')); 

         

    });   

  });

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="hidden"  value="hidden1">

<input type="hidden"  value="hidden2">

<input type="hidden" value="hidden3">

<input type="hidden">

<input type="hidden">


<button type="button" class="jsButton">Set & Get Hidden Value</button> <br/>

<lable id="lblHasValue"></lable> <br/>

<lable id="lblNotValue"></lable>


hidden如果hidden字段具有您获取和设置的值,您可以设置字段是否没有值


查看完整回答
反对 回复 2021-12-02
?
GCT1015

TA贡献1827条经验 获得超4个赞

我认为您应该检查每个输入类型的文本,而不是检查checked attribute基本上不是隐藏输入类型的复选框:


function myfunction(){

     $('#printarray').on('click', function() { 

         var array = []; 

         $("input[type=text]").each(function() { 

             if($(this).val().length !== 0) {

                 array.push($(this).val()); 

             } 

         }); 

         alert(array);

         $('#hdnship').val(array); 

     }); 

 }


查看完整回答
反对 回复 2021-12-02
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

您可以通过这种方式检查输入是否为空:


if( !$("input").val() ) {

   console.log("Empty");

}

或者


if( $("input").val().length === 0 ) {

   console.log("Empty");

}

或者,如果您确定它始终对文本字段元素进行操作,那么您可以使用“this.value”:


if( !$("input").value ) {

   console.log("Empty");

}


查看完整回答
反对 回复 2021-12-02
  • 3 回答
  • 0 关注
  • 140 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信