【发布时间】:2015-01-07 19:18:21
【问题描述】:
我已经为这个问题寻找了很长时间的解决方案,但找不到任何解决方案。
我从数据库中获取数据以在我的网站上显示一些项目。对于每个项目,我可以单击笔符号来编辑项目名称。我想发送每个 ajax 请求的日期,但总是获取第一个项目的数据。例如,如果我尝试编辑第二个项目“test”的名称,我会得到第一个项目“blubb”的数据
我尝试过的: - 使用的类不是 Id´s Call Ajax function from PHP foreach with form - 用过 $this Get Ajax variable from the PHP foreach loops
我的代码看起来像这样(简化):
< script type = "text/javascript" >
$(document).ready(function() {
$(".submit").click(function() {
//here are the problems, i only get the value of the first project
var new_project_name = $('.new_project_name').val();
var old_project_name = $('.old_project_name').val();
$.ajax({
type: "POST",
url: "php/edit/edit.php",
data: {
old_project_name: old_project_name,
new_project_name: new_project_name,
name: name
},
}).
done(function(response) {
blabla
});
});
}); < /script>
<?php foreach($db->query('SELECT * FROM portfolio ORDER BY position, id desc') as $row) { $name = $row['name']; ?>
<div class="rename">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td>Projektname:</td>
</tr>
<tr>
<td>
<input type="text" class="new_project_name" value="<?php echo $name;?>">
</td>
<input type="text" class="old_project_name" value="<?php echo $name;?>" hidden>
</tr>
<tr>
<td>
<input class="submit" type="submit" value="Umbenennen">
</td>
</tr>
</table>
</div>
<?php } ?>
感谢您的帮助。
【问题讨论】:
-
$('.new_project_name')将返回具有new_project_name类的所有内容的集合。我怀疑在集合上调用#val函数的目的是返回什么。要实现您想要的,您应该迭代集合以进行更改。 -
可以在jquery中使用
closest取对应的输入值,也可以使用id区分取对应的输入值 -
@.Siva.G :我尝试像这样使用最接近: var new_project_name = $(this).closest('.new_project_name').val();但这似乎行不通。