简体   繁体   English

验证垂直表单的表单输入

[英]Validating form inputs of a perticular form

I have the following code that makes sure inputs on forms aren't blank, however I have 2 forms on a page and only want this to check the inputs on the form called <form id="my_form" ... 我有以下代码可以确保表单上的输入不为空,但是我在页面上有2个表单,只希望它检查名为<form id="my_form"的表单上的输入...

var valid_form = true;
$$('input').each(function(item){
    if( item.value == '' ) valid_form = false;
});

Please can somebody tell me how to do this? 请有人告诉我该怎么做?

var valid_form = true;
$$('#my_form input').each(function(item){
    if( item.value == '' ) valid_form = false;
});

You can give a different ID for each of you forms like frmLogin and frmSearch and then when one of your forms is submitted you check only the inputs in it. 您可以为每个表单(例如frmLogin和frmSearch)指定不同的ID,然后在提交表单之一时仅检查其中的输入。

var valid_form = true;
$('form').submit(function () { 
 $(this).find('input').each(function(item){ 
   if( item.value == '' ) valid_form = false;  
 }) 
});

Or if you check your forms after a button has been clicked you can do it like this. 或者,如果您在单击按钮后检查表单,则可以这样做。

$('#yourButton').click(function () {
 $('#my_form input').each(function(item){ 
   if( item.value == '' ) valid_form = false;  
 }) 
});

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

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