在提交时验证两个表单


validate two forms on submit

我有两个表单,第一个表单包含文本框,第二个表单包含按钮。我想使用按钮(jquery或javascript)验证文本框:

<form name="contactus" method="post">
<input name="txtFirstname" id="Firstname" type="text" class="field" Value="First name*" style="width:300px" />
</form>
<form name="frm1" action="sendemail.php" method="post">     
<input id="send" type="image" src="images/sub.png" alt="Submit" style="float:right" />
</form>

当我的文本框不在"frm1"上时,我该如何做到这一点

以下是我在上面的评论中建议的两种方法的示例:

1)使用Javascript根据单击的按钮更改表单的操作属性:

<form id='the_form' action='' method='post'>
  <input type='text' name='text_input' />
  <input type='button' id='button_1' value='Button 1' />
  <input type='button' id='button_2' value='Button 2' />
</form>
<script type='text/javascript'>
  var theForm = document.getElementById('the_form');
  $('#button_1').click(function() {
    theForm.action = 'script1.php';
    theForm.submit();
  });
  $('#button_2').click(function() {
    theForm.action = 'script2.php';
    theForm.submit();
  });
</script>

2)将所有提交处理程序代码保存在一个脚本中(推荐):

HTML:

<form id='the_form' action='script.php' method='post'>
  <input type='text' name='text_input' />
  <input type='submit' name='submit_1' value='Submit' />
  <input type='submit' name='submit_2' value='Submit' />
</form>

PHP:

<?php
  if (isset($_POST['submit_1'])) {
    echo 'You clicked button 1';
  } else if (isset($_POST['submit_2'])) {
    echo 'You clicked button 2';
  }

你试过这个吗?

$('#send').click(function(){
    var textVal = $('#Firstname').val();
    //your validation code goes here
});