【问题标题】:javascript check function for a html formhtml表单的javascript检查功能
【发布时间】:2012-07-08 08:44:58
【问题描述】:

我的 HTML 表单(姓名、电子邮件、主题、消息)有问题,我无法验证表单是否正确填写。我想在提交表单时调用 javascript 函数,并在字段未正确填写时发出警报。

我的问题是提交按钮,因为我的老板说我必须使用他自己的php函数来插入提交按钮。 php函数看起来像:

function normal_button($text, $width, $param = '', $class = 'button-blue')
{
    $content = '<div class="'.$class.'" style="width:'.$width.'px;" '.$param.'>'.$text.'</div>';

    return $content;
}

以及我如何调用函数:

echo normal_button('Send it', '100', 'onclick="document.getElementById(\'emailForm\').submit();" ');

我尝试在&lt;form action='email.php' method='post'&gt; HTML 表单中调用声明onSubmit="return checkForm(this)" 的js 函数,但我无法“捕获”提交。我也尝试用 jquery 捕捉它,$(document).ready(function() { $('.normal_button').click(function() { }); ); 但如果字段未正确完成,我无法返回 false。

我必须使用他的 PHP 函数来插入提交按钮,但是我怎样才能“捕捉”这些字段,我怎样才能验证它们呢?有什么建议么?谢谢!

【问题讨论】:

  • 您不是在插入提交按钮,而是在插入
  • 并显示整个表单和 checkForm(this) 函数

标签: php jquery html forms validation


【解决方案1】:

首先,为该表单和 sbmit DIV 按钮设置 ID。

<form action='email.php' method='post' id='mailForm'>

echo normal_button('Send it', '100', 'id="mailFormSubmit"');

然后用 jQuery 检查:

$('#mailFormSubmit').click(function(){
    // fields check code goes here

....

    // if all fields are OK, then submit form
    $('#mailForm').submit();
});

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签