简体   繁体   English

如何检查用户是否尚未将数据输入表单(用于发送)?

[英]How to check if the user has not entered the data to a form (befor sending)?

I have some input form on names: owner, number, city 我有一些关于姓名的输入表格:所有者,号码,城市

<input id="id_owner" type="text" name="owner" maxlength="250" />
<input id="id_number" type="text" name="number" maxlength="250" />
<input id="id_city" type="text" name="city" maxlength="250" />

How to check if the user has not entered the data to a form (befor sending) that does not show this dialog from this code: 如何检查用户是否尚未从以下代码将数据输入未显示此对话框的表单(用于发送):

<a type="submit" name"save-continue-to-review" data-toggle="modal" data-target="#dialog" href=""
class="btn primary btn-primary" title="Order">Order
</a>

and it will show another 它将显示另一个

Here is full code: http://wklej.org/id/927806/ 这是完整的代码: http : //wklej.org/id/927806/

Eventually you'll be able to use HTML5 form validation. 最终,您将能够使用HTML5表单验证。 But until then, use some jQuery code like this. 但是在那之前,请使用一些这样的jQuery代码。 (only because you tagged the question with jQuery. You could potentially do it with vanilla JS.) (仅是因为您使用jQuery标记了问题。您可能可以使用Vanilla JS进行此操作。)

(un-tested code, but should work) (未经测试的代码,但应该可以)

var fields = $('input')

$('form').submit(function(e){
    e.preventDefault()
    valid = true
    fields.each(function(){
        if ($(this).val() == null) {
            valid = false
        }
    });

    if (valid == true) {
        $('form').submit()
    } else {
        alert("At least one field was not valid!")
    }
});
if ( !$(this).val() ) {
  valid = false
}

maybe this post is useful for you 也许这篇文章对您有用

1) Add this on your form 1)在您的表单上添加

onsubmit="return validateForm(this);" 

2)The validate function (checks if fields are empty) 2)验证功能(检查字段是否为空)

function validateform(formObj)
{
  inputs = formObj.GetElementsByTagName('input');

  for(i=0; i < inputs.length; i++)
  {
    if($.trim(inputs[i].value) == '')
    {
       alert('Field: ' + inputs[i].name + ' is empty!');
       return false;
    }
  }  
  return true;
}

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

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