表单不会被if语句检查

时间:2013-07-23 17:05:41

标签: javascript jquery ajax

我有两个标识为formAcomments的表单,我想通过AJAX提交它们。但ifelse此处不会检查表单。我总是很警觉hello3

JS:

function submitformbyajax() {
    var currentForm = $(this);
    if (currentForm.attr("id") == 'formA') {
        $.ajax({
            type: 'post',
            url: 'commentformhandler.php',
            data: $("form").serialize(),
            success: function() {
                $("#refresh").load("commentform.php #refresh");
            }
        });
    } else if (currentForm.attr("id") == 'comments') {}
    alert("hello3");
    return false;
}

该函数由

调用
    <div>
    <form name="formA" id="formA" action="" method="" onsubmit="return     submitformbyajax();">
        <textarea name="comment" id="commentform" style="width:90%; height:45px;"></textarea>
        <input type="submit" name="submit" value="submit" id="submitbtn" />
        <input type="hidden" name="onid" value="2" id="submitbtn"/>
    </form>
</div>

这是完整的演示页面....

<?php

?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">   </script>
<script>
    function submitformbyajax (){
var currentForm = $(this);
  if (currentForm.attr("id") == 'formA' ) {
        $.ajax({type: 'post',
                url: 'commentformhandler.php',
                data: $("form").serialize(),
                success: function(){
                $("#refresh").load("commentform.php #refresh");
                alert ("hello1");
                }
        } );
}
else if  (currentForm.attr("id") == 'comments') {
        alert("hello2");
    }
    alert ("hello3");
    return false;
}
</script>
    <title>
        comment forms..
    </title>
</head>
<body>
<div>
    <form name="formA" id="formA" action="" method="" onsubmit="return submitformbyajax();">
        <textarea name="comment" id="commentform" style="width:90%; height:45px;"></textarea>
        <input type="submit" name="submit" value="submit" id="submitbtn" />
        <input type="hidden" name="onid" value="2" id="submitbtn"/>
    </form>
</div>
<div id="refresh">
    <?php
        include_once('databaseconnection.php');
        $selectdata=mysql_query("select * from `fetwork_view` ");
        while($selectedinarray=mysql_fetch_array($selectdata)){ ?>
            <table>
                <tr>
                    <td>
                        <?=$selectedinarray['view']?>
                    </td>
                </tr>
                <tr>
                    <td>
        <form name="comment" id="comments" action="" method="">
        <textarea name="comment" id="commentform" style="width:70%; height:25px;"></textarea>
        <input type="submit" name="submit" value="submit" id="submitbtn" />
        <input type="hidden" name="onid" value="2" id="submitbtn"/>
        </form>
                    </td>
                </tr>
            </table>

    <?php } ?>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我认为你应该使用

$("form#formA").submit(function(){
  alert(1);
});

答案 1 :(得分:0)

无论alert(...)测试的条件如何,都会执行if语句。它在if之后立即执行。

请注意,ajax不会重定向代码的“流程”。浏览器将“启动”AJAX请求并继续。然后,在收到服务器的响应后 - 将执行AJAX回调函数。

更新

将表单“传递”到submitformbyajax函数添加this作为参数:

<form name="formA" id="formA" onsubmit="submitformbyajax(this);">

JS:

function submitformbyajax(your_form) {
    var currentForm = $(your_form);