gpt4 book ai didi

javascript - jQuery/Ajax 在警报中正确发送 "data-"值,但无法将其附加到 php 中的特定 div 中

转载 作者:行者123 更新时间:2023-11-30 19:06:18 24 4
gpt4 key购买 nike

我目前有一个 html 按钮,它的正下方有一个 div,我想在其中发布某些数据。

这两个元素都在 php for 循环中。

<?php for($i = 0 ; $i < 10; $i++){ ?>
<div class="form-group">
<label class="col-md-4 control-label" for="ver"></label>
<div class="col-md-4">
<button data-dnipass="<?= $dni?>" class="ver" name="ver" class="btn btn-primary">
Ver líneas
</button>
</div>
</div>
<div id ="<?= $i?>" class="table userInfo" data-formpost="<?= $dni?>"></div>
<? } ?>

$dni 是我从数据库中获取的值,并且对于每次迭代都是唯一的。

然后我有一个 ajax 脚本执行以下操作

$(document).ready(function(){
$(".ver").click(function(event){
var pulsado = $(this).data("dnipass"); //this is properly asigning the data
alert(pulsado); //this properly launches an alert with my desired value
$(this).data("formpost").append(pulsado); //im trying to append my variable to my ".userinfo" div but im unable to do so, since I dont know how to identify so
});
})

如何将 var pulsado 附加到类为“userinfo”的 div 中? var pulsado 的内容是一个简单的字符串,例如“x1234”或“x4321”......

最佳答案

试试下面的代码

    $(".ver").click(function(event){
var pulsado = $(this).data("dnipass"); //this is properly asigning the data
$(this).closest('.form-group').next('.userInfo').append(pulsado);
});

closest 找到 div,您的 button 被包裹在该 div 下。 next 然后找到带有 userInfo 类的直接 div

关于javascript - jQuery/Ajax 在警报中正确发送 "data-"值,但无法将其附加到 php 中的特定 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58950137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com