简体   繁体   English

jQuery函数仅在文本框更改时运行

[英]jQuery function only running when text box changes

I have a form that I need to do some validation on, my jquery seems to be working except when a user just enters the page and submits without making any entry into a text box. 我有一个需要进行验证的表单,我的jquery似乎可以正常工作,除非用户只是进入页面并提交而没有在文本框中进行任何输入。 Here is my HTML: 这是我的HTML:

...
<% while (rs.next()) { %>
<input type="text" name="name" />
<%  }  %>
<input type="text" id="fafsaNbrFam" name="fafsaNbrFam" value="<%=nbrFam%>" class="hidden" />
....

and my script: 和我的脚本:

 $("#submit").click(function(e) {
        var numEntries = 0;
        var fafsaNbr = 0;

        $("input[name^='name_']").each(function() {
          if (this.value) {
            numEntries++;
          }
        });

        fafsaNbr = parseInt($("input[name='fafsaNbrFam']").val());


        if (fafsaNbr > numEntries && !confirm("WARNING, numbers don't match.")) {
          e.preventDefault();
        }
      });

So basically if their number of text boxes filled with names isn't >= the hidden input value then the kick back happens, and it works except when the user never enters anything into the text box. 因此,基本上,如果用名称填充的文本框数量不是> =隐藏的输入值,则会发生反冲,并且除非用户从不向文本框中输入任何内容,否则反跳有效。 What event could I use instead of $("input[name^='name_']").each(function() { to accomplish this? 我可以使用什么事件代替$(“ input [name ^ ='name _']”)。each(function(){来完成此操作?

According to me it wont work because when textbox is empty then 据我说,这将无法正常工作,因为当文本框为空时

$("input[name^='name_']").each(function()

parseInt($("input[name='fafsaNbrFam']").val());

.each() and val() return null. .each()和val()返回null。 Due to this your control did not run ahead. 因此,您的控件无法继续运行。

One thing you can do: 您可以做的一件事:

document.getElementById("fafsaNbrFam")==null

using this you can check the value as true or false. 使用此功能,您可以将值检查为true或false。 Because in case of empty textbox when you write the document.getElementById("fafsaNbrFam") or $("#fafsaNbrFam") it return value null. 因为在编写document.getElementById(“ fafsaNbrFam”)或$(“#fafsaNbrFam”)时,如果文本框为空,则它返回值null。

And, null.val() or null.each() gives error. 并且,null.val()或null.each()给出错误。

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

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