jquery 中的新功能:如何在 jquery 中单独提交表单?
这是 jQuery 中的标准表单提交器:
<script>
$(document).ready(function(){
$("form#submit").submit(function() {
var fname = $('#fname').attr('value');
var lname = $('#lname').attr('value');
$.ajax({
type: "POST",
url: "report.php",
data: "fname="+ fname +"& lname="+ lname,
success: function(){
$('form#submit').hide(function(){
$('div.success').show();
});
}
});
return false;
});
});
</script>
以下是表单:
<form id="submit" name="submit">
<input id="fname" name="fname" size="20" type="hidden" value="aword"> <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">
<button id="send">send</button>
</form>
<form id="submittwo" name="submittwo">
<input id="fname" name="fname" size="20" type="hidden" value="anoterword"> <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">
<button id="send">send</button>
</form>
report.php:
<?php
$fname = htmlspecialchars(trim($_POST['fname']));
$lname = htmlspecialchars(trim($_POST['lname']));
$myFile = "reports.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "\n".$fname."\n";
fwrite($fh, $stringData);
$stringData = $lname."\n";
fwrite($fh, $stringData);
fwrite($fh, $theData);
fclose($fh);
?>
将会有更多使用 PHP 循环的表单。我想做的是将它们分开提交。知道如何或可以告诉我在哪里可以找到答案吗?
This a standard form submitter in jQuery:
<script>
$(document).ready(function(){
$("form#submit").submit(function() {
var fname = $('#fname').attr('value');
var lname = $('#lname').attr('value');
$.ajax({
type: "POST",
url: "report.php",
data: "fname="+ fname +"& lname="+ lname,
success: function(){
$('form#submit').hide(function(){
$('div.success').show();
});
}
});
return false;
});
});
</script>
and here are the forms:
<form id="submit" name="submit">
<input id="fname" name="fname" size="20" type="hidden" value="aword"> <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">
<button id="send">send</button>
</form>
<form id="submittwo" name="submittwo">
<input id="fname" name="fname" size="20" type="hidden" value="anoterword"> <font class='black'>report:</font>
<input id="lname" name="lname" size="40" type="text">
<button id="send">send</button>
</form>
report.php:
<?php
$fname = htmlspecialchars(trim($_POST['fname']));
$lname = htmlspecialchars(trim($_POST['lname']));
$myFile = "reports.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "\n".$fname."\n";
fwrite($fh, $stringData);
$stringData = $lname."\n";
fwrite($fh, $stringData);
fwrite($fh, $theData);
fclose($fh);
?>
There will be more forms looped with PHP. What I want to do is to submit them separately. Any idea how or can you tell me where I can find an answer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我已经很好地理解了这个问题,这可以帮助
这里是一个小提琴 http://jsfiddle.net/fG8T3/
iff i have understood the question well this can help
here is a fiddle http://jsfiddle.net/fG8T3/
您可以只使用一种表单,但更改字段名称
现在您可以将 GET 数据作为数组进行迭代:
注意:ID 应该是唯一的。
You could just use one form, but change the names of your fields
Now you can iterate through the GET data as an array:
Note: ID's should be unique.
目前尚不清楚您在哪里遇到问题,但如果您尝试在不离开当前页面的情况下提交它们,您可能正在寻找 jQuery 表单插件。它使提交表单和显示结果变得更加容易,因此您无需像在发布的代码中那样手动执行此操作。
It's not very clear where you're having the problem but if you're trying to submit them without leaving the current page you may be looking for the jQuery Form plugin. It makes it a bit easier to submit forms and display the results so you won't be doing it manually as you are in the code you posted.