Javascript if语句不检查表单
我有两个id为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
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);