【发布时间】:2025-12-26 03:30:10
【问题描述】:
我有一个从 sql 选择生成数据的 foreach。当用户点击 div 时,会出现一条文本消息“points ganed”,表单内的数据将被提交。
整个脚本可以工作,但它总是在 4 生成时提交最后一个表单……他没有针对正确的表单。
有办法锚定要提交的正确表单吗?在此先感谢大家。
<script>
function(response) {
$('#mydiv').text('Points gained');
$("#form").submit()
}</script>
foreach ($usermeta as $post) {
<form method="post" id="form" action="<?php the_permalink(); ?>">
<div id="mydiv">
click here to earn points.
</div>
<input type="hidden" name="points" id="points" value="<?php echo $post->points; ?>" class="regular-text" />
</form>
}
【问题讨论】:
-
问题出在表单 id 上,因为您对所有表单都使用了相同的 id(顺便说一句,这不是有效的 w3c 标准),解决方案是使用 $(this).parents("form:first") .submit(),但我认为无论如何您都应该为每个表单提供唯一 ID