Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript if语句不检查表单_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript if语句不检查表单

Javascript if语句不检查表单,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有两个id为formA和comments的表单,我想通过AJAX提交它们。但是这里的if和else没有检查表单。我总是得到警告hello3 JS: 该函数由调用 <div> <form name="formA" id="formA" action="" method="" onsubmit="return submitformbyajax();"> <textarea name="comment" id="commentfo

我有两个id为
formA
comments
的表单,我想通过AJAX提交它们。但是这里的
if
else
没有检查表单。我总是得到警告
hello3

JS:

该函数由调用

    <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>

函数submitformbyajax(){
var currentForm=$(此);
if(currentForm.attr(“id”)==“formA”){
$.ajax({type:'post',
url:'commentformhandler.php',
数据:$(“表单”).serialize(),
成功:函数(){
$(“#刷新”).load(“commentform.php#刷新”);
警报(“hello1”);
}
} );
}
else if(currentForm.attr(“id”)=“comments”){
警惕(“hello2”);
}
警惕(“hello3”);
返回false;
}
评论表格。。
我认为您应该使用

$("form#formA").submit(function(){
  alert(1);
});
您的
警报(…)
语句将被执行,无论
if
测试的条件如何。如果,它将在该
之后立即执行

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

更新:

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

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

警报仅用于检查我不需要任何警报我只希望我的表单通过if和else进行检查,然后进行相应的处理…您不应该使用具有相同ID的表单。使用类代替。表单有两个不同的ID formA和comments…表单未处理。。警报不是问题。问题是表单未处理。如果或否则,则不进入if或else语句…1。
submitformbyajax
是如何调用的?2.您在
currentForm.attr(“id”)
中看到了什么?请在问题的#1中添加答案。答案很有用。。。表单正在被检查,但现在textarea中的值没有被传递…您是否应该使用
数据:currentForm.serialize()
而不是
$(“表单”).serialize()
?隐藏字段中的值被传递并保存在数据库中,但textarea中的值没有传递。(仅在传递空值之后才第一次传递值。)请帮助。
<form name="formA" id="formA" onsubmit="submitformbyajax(this);">
function submitformbyajax(your_form) {
    var currentForm = $(your_form);