【发布时间】:2009-04-06 05:35:19
【问题描述】:
我正在创建一个动态表单,用户可以在其中向表单添加一组输入。 html 看起来像这样:
<form>
<input id="title1" class="title" name="title1" type="text" value="">
<input id="productionCompany1" name="productionCompany1" type="text" value="">
<input id="year1" name="year1" type="text" value="">
<input id="role1" name="role1" type="text" value="">
<div id="newCredit"> </div>
<a href="#" id="addCredit">add another credit</a>
</form>
当用户单击 id 为“addCredit”的链接时,会调用以下 jQuery 脚本:
$(document).ready(function() {
var $ac = $('#addCredit');
$ac.click(function() {
/* the following two lines are where the problem lies? */
var $credInt = $(this).prev(".title");
$.get("addCredit.php", {num: $credInt},
function(data){
$('#newCredit').append(data);});
return false;
});
});
jQuery 函数查询名为“addCredit.php”的 php 文件,如下所示:
<?php
$int = $_GET["num"];
$int = substr($int, -1);
$int++;
?>
<input id="title<?php echo $int;?>" class="title" name="title<?php echo $int;?>" type="text" value="">
<input id="productionCompany<?php echo $int;?>" name="productionCompany<?php echo $int;?>" type="text" value="">
<input id="year<?php echo $int;?>" name="year<?php echo $int;?>" type="text" value="">
<input id="role<?php echo $int;?>" name="role<?php echo $int;?>" type="text" value="">
我的问题是正确设置 javascript 变量 $credInt 以便可以将其发送到 addCredit.php 页面并相应地更新表单字段。我还需要确保每次附加表单时,发送的下一个值是递增的值。
我有什么想法可以做到这一点吗?感谢您的帮助。
【问题讨论】: