javascript 如果条件为真,则提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18077551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:40:26  来源:igfitidea点击:

Submit form if condition is true

phpjavascriptjqueryhtml

提问by mayank

I have this form:

我有这个表格:

<form action="http://targatesite.com" target="_blank" method="post" name="xyz" id="abc">
      <input type="hidden" name="email" value="user_email" />
      <input type="submit" name="sbmt" id="sbmt" value="user_value" class="user_class"/>
</form>

Suppose there is a variable VAR. The form can be submitted only when the value of VARis 1.

假设有一个变量VAR。只有当VAR 的值为1时才能提交表单。

In detail On click on submit it will firstly check the value of VARand if it returns truefrom will submitted and redirect to page given in action. And if it returns Falseit will show a message that Please check value of VAR

详细点击提交时,它将首先检查VAR的值,如果它返回true,则将提交并重定向到操作中给出的页面。如果它返回False它将显示一条消息,请检查 VAR 的值

ImpVAR is not the field of form. We are getting this value from database.

ImpVAR 不是形式领域。我们从数据库中获取这个值。

回答by Somnath Kharat

Add onSubmit="return submit();"

添加 onSubmit="return submit();"

<form action="http://targatesite.com" target="_blank" method="post" name="xyz" id="abc"> <input type="hidden" name="email" value="user_email" />    
    <input type="submit" name="sbmt" id="sbmt" value="user_value" class="user_class" onSubmit="return submit();"/> 
    </form>

Try this Script :

试试这个脚本:

 function submit(){
            if(var==1){
                return true;
           }
           else{
               alert("Please check value of VAR")
               return false;
           }
    }

回答by Sergio

$('#abc').on('submit', function(){
     if(VAR == 1){
        return true;  
     } 
   alert('Please check value of VAR');
   return false;
});

This will work. The only thing you need to check is that VARis in the scope of this function.

这将起作用。您唯一需要检查的VAR是在此函数的范围内。

回答by bipen

try this

试试这个

var MYVAR=true;
$('#abc').submit(function(){
     if(MYVAR){
        return true;  
     } 
   alert('check the value of VAR');
   return false;
});

回答by Prashant Parekh

Yes you can check VAR variable value from database,

是的,您可以从数据库中检查 VAR 变量值,

when you have to submit form, you can code like this

当你必须提交表单时,你可以这样编码

if($_POST['sbmt']){
 $query = 'SELECT var FROM tablename';
 // here you can check var value
 if(VAR==1){
  //submit your form
 } else {
  $error_msg = 'Please check value of VAR';
 }
}

print this $error_msg whenever you want to show.

每当您想显示时打印此 $error_msg 。