两个不同的提交按钮的JavaScript验证

时间:2015-03-05 09:08:54

标签: javascript html forms

我的表格如下: -

****<form name = "query" id="myDiv" method = "POST" enctype="multipart/form-data" action = "Response.php" onsubmit = "return commentValidationTest()">****

在此表单中有两个提交按钮

<input type="Submit" name ="sub" id ="sub" value="Save" class="btn btn-info btn-sm" >

<input type="Submit" name ="submit" id ="submit" value ="Submit" align = "center" style="width: 80px; float: center;">

当单击两个提交按钮时,表单数据的验证正在commentValidationTest()中完成,该验证是用javascript编写的,如果验证是“好的”,那么php文件“响应”。 PHP&#39;被称为。

在我的commentValidationTest()中,我需要检测单击了哪个提交按钮,并根据单击的按钮执行表单数据的验证。

我这样做:

var sub = document.getElementById('sub');

if(sub.onclick==true)
{

Validations...
}

5 个答案:

答案 0 :(得分:0)

编写两个返回true或false的函数,并在onclientclick属性上调用这些函数。

<input type="Submit" name ="sub" id ="sub" onclientclick="return fun1();"
value="Save" class="btn btn-info btn-sm" >



<input type="Submit" name ="submit" id ="submit" onclientclick="return
fun2();"  value ="Submit" align = "center" style="width: 80px; float:
center;">

在函数中

fun1()
{
    check for validations 
    if(validation==success)
    {
       return true;
    }
    else
    {
       return false;
    }
}

same in fun2()

答案 1 :(得分:0)

如果使用jQuery,可以编写类似

的内容
<button id="btn1" onclick="commentValidationTest(this)">
<button id="btn2" onclick="commentValidationTest(this)">

function commentValidationTest(el) {
  alert($(el).attr('id')); 
}

然后检查将会点击按钮信息的'this'对象。

答案 2 :(得分:0)

使用此: -

如果你正在形成这个。

****<form name = "query" id="myDiv" method = "POST" enctype="multipart/form-data" action = "Response.php" >****

有输入

<input type="Submit" name ="sub" id ="sub" value="Save" class="btn btn-info btn-sm" >

<input type="Submit" name ="submit" id ="submit" value ="Submit" align = "center" style="width: 80px; float: center;">

使用此代码: -

var buttonpressed; 
$('.classAndDocsFormSubmit').click(function() { 
    buttonpressed = $(this).attr('name') 
});
$('#addClassAndDocs').submit(function() { 
    //alert('button clicked was ' + buttonpressed) 
        if(buttonpressed=="sub") {
      commentValidationTest()

          } else {

          commentValidationTest2()
         }

    return(false) 
});

答案 3 :(得分:0)

我有一个类似的情况,表单同时具有注册和登录功能并以这种方式处理

 <form id="manual_login" >
     <button type="submit" id="loginButton" name="login" value="login">Login</button>
     <button type="submit" id="registerButton" name="register" value="new" align="left">Join</button>
 </form>

获取按钮的值,以便您知道单击了哪一个:

$('#manual_login button').click(function(event) {
    var test = $(this).data('clicked',$(event.target));
    op = test.val();
});

然后根据按钮执行您需要做的事情:

$('#manual_login').on('submit', function(event){
        event.preventDefault(); //prevent the default submit
        if(op == "new"){
            //code here.
            // commentValidationTest()
        } else {    
            var form = $(this).serialize();
            commentValidationTest(form);
        }
    });

答案 4 :(得分:0)

以下是关于在按钮上使用onclick的工作:

<form onsubmit="return doStuff()">
  <input type='submit' name='save' value='Save' onclick='who(this.name);'/>
  <input type='submit' name='submit' value='Submit' onclick='who(this.name);'/>
</form>

<script>
var isSave = false;

var who = function(name) {
  isSave = 'save' === name;
};

var doStuff = function() {
  if(isSave) {
    //
  } else {
    //
  }
  return true; //false, if do not want to submit
};
</script>
相关问题