gpt4 book ai didi

javascript - Jquery - 在追加元素上插入值

转载 作者:行者123 更新时间:2023-11-28 14:37:09 25 4
gpt4 key购买 nike

如何在 <input type="text"> 上插入值但这个元素来自append方法。这是代码

HTML

<input type="email" name="invite_people" placeholder="Invite someone">
<a href="#!" class="add_people">Add People</a>
<div class="people_invite">
<!-- Here is <input type="text" name="people_invited" readonly> is added by jquery -->
</div>

Jquery

$(".add_people").click(function () {
var orang_yg_diinvite = $("input type='text' name='invite_people'>").val();
$(".people_invite").append("<input type='text' name='people_invited' readonly>");
});

追加<input type="text" name="people_invited" readonly>成功里面<div class="people_invite"> ,但我想做<input type="text" name="people_invited" readonly>具有来自 var orang_yg_diinvite 的值。我怎样才能做到这一点?请帮助我:)

最佳答案

有多种方法可以做到这一点:

Just directly add it to the HTML of the input you're appending

$(".people_invited").append("<input type='text' value='"+orang_yg_diinvite+"'>");

或者

Edit your input's value after it was appended

$(".people_invited").append("<input type='text'>");
$(".people_invited>input").val(orang_yg_diinvite);

演示:

$(".add_people").click(function () {
var orang_yg_diinvite = $("input[name='invite_people']").val();
$(".people_invited").append("<input type='text' value='"+orang_yg_diinvite+"'>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="email" name="invite_people" placeholder="Invite someone">
<a href="#!" class="add_people">Add People</a>
<div class="people_invited">
</div>

关于javascript - Jquery - 在追加元素上插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49506634/

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