我在我的PHP文件末尾有这个:
<script type="text/javascript>"
$("#id").on("click", "#otherId", function(e)
{
var html = "<input class='id' type='text' size='5' />";
var row = $(this).closest("#addoid").html(html);
row.find("input").focus();
row.find('input').change( function(e)
{
var value = $(this).val();
var fid = $("input#idName").val();
$.ajax({
type: "POST",
url: "page.php",
data: { entryId: fid, val: value }
})
.done(function( msg ) {
alter("data: " + msg);
});
});
</script>
</body>
The html is something like this:
<div id="id">
<div id="otherId">
<input id="idName" type="hidden" value="1234" />
<button type="button" id="otherId">Add</button>
</div>
<div id="otherId">
<input id="idName" type="hidden" value="1235" />
<button type="button" id="otherId">Add</button>
</div>
<div id="otherId">
<input id="idName" type="hidden" value="1236" />
<button type="button" id="otherId">Add</button>
</div>
</div>
我将为您翻译以下内容:
我之前留下了jQuery代码,但基本上,当按下添加按钮时,它会变成一个输入框,如果用户将ID放入输入框中,则在模糊状态下显示链接,如果没有输入任何内容则返回到添加按钮。
到目前为止它是有效的,然而...
var fid = $("input#idName").val();
正在取下一个ID(不是1234,而是发布1235)。
我对 jQuery 不熟悉。我已经搜索了很多东西并尝试了几种不同的方法,但我一无所获。
谢谢。
补充说明
为了更清楚地说明,我有一个表格正在通过 foreach
循环(使用 PHP)进行填充,它正在从数据库中提取记录。
看起来像这样:
<div id="list">
<table>
<?php foreach ($data as $value):?>
<tr>
<td>
<div class="row">
<button class="add">Add</button>
<input class="hiddenId" type="hidden" name="hiddenName" value="<?php echo $value['id']?>" />
</div>
</td>
</tr>
<?php endforeach?>
</table>
</div>
如我之前所解释的那样,当单击“添加”按钮时,它会变成一个输入字段。
当输入字段发生更改时,我需要ajax发送一个请求,其中包含两个输入字段的值。
我遇到的问题是,我无法获得隐藏输入的正确值。 jQuery抓取了另一行的值,而不是正确的行。
我似乎无法解决这个问题,实际上尝试了许多不同的方法,包括一些可能被认为是不寻常的方法。
感谢任何帮助。
id
必须唯一。 - Oleksandr T.<div id="otherId">
更改为<div class="otherId">
,并将$("#id").on("click", "#otherId", function(e)
更改为$("#id").on("click", ".otherId", function(e)
。在输入ID中执行相同的操作。 - Flávio Schuindt<input type="hidden"
的ID需要唯一吗? - Mikolaj<a class="add" data-id="id">Add</a>
和jquery:$(this).data("id")
。然后,我能够将数据库表的记录ID和用户输入的值(以更新记录)传递给数据库。 - Mikolaj