简体   繁体   English

需要帮助选择jquery中没有值的输入字段

[英]Need help selecting input fields with no value in jquery

I am trying to write a script that checks if any of the input fields has no value, and when it does it needs to display an alert message and return false. 我正在尝试编写一个脚本,检查任何输入字段是否没有值,并且在执行该脚本时需要显示警报消息并返回false。

The input field below is just a small example of my real world application. 下面的输入字段只是我的真实应用程序的一个小示例。 There maybe more then 5 input fields with a value already in it, and 1 or more input fields that have no value in it 可能已经有5个以上输入值已经在其中的输入字段,以及1个或多个其中没有值的输入字段

The input fields that has no value are only the ones with returned data(html code) from a ajax post 没有值的输入字段仅是从ajax帖子返回了数据(html代码)的字段

The problem is that the script is not displaying an alert message when one of the input fields has no value. 问题在于,当输入字段之一没有值时,脚本不会显示警报消息。 (The one with the ajax returned data) (带有ajax的返回数据)

Any help would be really appreciated. 任何帮助将非常感激。

Below is the code that I am using. 下面是我正在使用的代码。

HTML CODE HTML代码

<input type='text' class='option_category' name='option_category_config[35196][option_cat_name]' value='Size of Neapolitan' >

<input type='text' class='option_category' name='option_category_config[35197][option_cat_name]' value='Choice of Whole Pizza Toppings' >


//Empty input field. This is not even shown on the HTML source code. I needed to copy and paste it from firebug. I am not sure if that is the reason it doesn't return false

<input class="option_category" type="text" placeholder="Enter category name" value="" name="option_category_config[35236][option_cat_name]">

jquery code jQuery代码

 var option_cat = $('.option_category[name*="option_category_config"]');

$('input[name="update_menu_options"]').on('click', function(){



        option_cat.each(function(){

      var check_val =  $(this).val();
      console.log(check_val);
        if(!check_val){
           alert('there are missing input(s)');
           return false;
           }


        });



              return false;

        });

Odd -- with the code you provided, I'm seeing the expected behavior: http://jsfiddle.net/DuncanMack/33bwdLqm/ 奇怪-使用您提供的代码,我看到了预期的行为: http : //jsfiddle.net/DuncanMack/33bwdLqm/

If the items are being loaded dynamically, then make sure you've recalculated the value for option_cat. 如果项目是动态加载的,请确保已重新计算option_cat的值。 Without the complete code I can't be sure, but it sounds like you're running this on document.ready() -- which would only calculate for the items already present -- and then dynamically loading additional input values. 没有完整的代码,我不能确定,但​​这听起来像是您在document.ready()上运行它(仅计算已存在的项目),然后动态加载其他输入值。 Easy enough to fix -- just move var option_cat = $('.option_category[name*="option_category_config"]'); 修复起来很容易-只需移动var option_cat = $('.option_category[name*="option_category_config"]'); into the onClick event. 进入onClick事件。

You are using a Boolean comparison which can provide inconsistent results, for example entering 0 would return a false even though technically there is a value. 您正在使用布尔比较,该布尔比较可能提供不一致的结果,例如,即使从技术上讲,有一个值,但输入0将返回false。 Change this part of your code: 更改代码的这一部分:

if(check_val.length == 0){
    alert('there are missing input(s)');
    return false;
}

Here is the answer: javascript validation for empty input field 这是答案: 空输入字段的javascript验证

<script type="text/javascript">
function validateForm()
{
var a=document.forms["Form"]["ans_a"].value;
var b=document.forms["Form"]["ans_b"].value;
var c=document.forms["Form"]["ans_c"].value;
var d=document.forms["Form"]["ans_d"].value;
if (a==null || a=="",b==null || b=="",c==null || c=="",d==null || d=="")
  {
  alert("Please Fill All Required Field");
  return false;
  }
}

<form method="post" name="Form" onsubmit="return validate()" action="">
<textarea cols="30" rows="2" name="ans_a" id="a">
<textarea cols="30" rows="2" name="ans_b" id="b">
<textarea cols="30" rows="2" name="ans_c" id="c">
<textarea cols="30" rows="2" name="ans_d" id="d"></textarea>
</form>

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

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