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

这是我隐藏的输入字段


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

     }); 

 }


慕妹3242003
浏览 158回答 3
3回答

弑天下

你也可以试试这个检查是否为hidden空$(document).ready(function(){&nbsp;&nbsp;&nbsp; $(".jsButton").click(function(){&nbsp; &nbsp; &nbsp; &nbsp;var array = [],emptyvalue=[];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$("input[type=hidden]").each(function()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($(this).val().length !== 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array.push($(this).val());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;emptyvalue.push("value");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#lblHasValue').html(array.join(','));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#lblNotValue').html(emptyvalue.join(','));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp; });<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input type="hidden"&nbsp; value="hidden1"><input type="hidden"&nbsp; 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字段具有您获取和设置的值,您可以设置字段是否没有值

GCT1015

我认为您应该检查每个输入类型的文本,而不是检查checked attribute基本上不是隐藏输入类型的复选框:function myfunction(){&nbsp; &nbsp; &nbsp;$('#printarray').on('click', function() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var array = [];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$("input[type=text]").each(function() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($(this).val().length !== 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array.push($(this).val());&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert(array);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$('#hdnship').val(array);&nbsp;&nbsp; &nbsp; &nbsp;});&nbsp;&nbsp;}

阿波罗的战车

您可以通过这种方式检查输入是否为空:if( !$("input").val() ) {&nbsp; &nbsp;console.log("Empty");}或者if( $("input").val().length === 0 ) {&nbsp; &nbsp;console.log("Empty");}或者,如果您确定它始终对文本字段元素进行操作,那么您可以使用“this.value”:if( !$("input").value ) {&nbsp; &nbsp;console.log("Empty");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript