我有这样的密码
<form asp-controller="Products" asp-action="AddComment" asp-route-id="@Model.Id" id="CommentForm">
<div class=" w-100 mt-1 ml-5 mr-5 d-block">
<div class="d-inline-block">
<lable class="product-subtitle">Name</lable>
<input id="Name" type="text" class="reply-comment" value="@fullname" disabled />
</div>
<div class="d-inline-block ">
<lable class="product-subtitle">Email</lable>
<input id="Email" type="email" class="reply-comment" value="@userEmail" />
</div>
</div>
<div class="mt-1 ml-5 mr-5 d-block ">
<textarea rows="3" class="reply-comment" id="Comment" name="Comment"></textarea>
</div>
<div class="mt-1 ml-5 mr-5 mb-4 d-flex justify-content-end">
<div>
<a class="btn btn-primary btn-buy d-flex justify-content-center add-comment" data-id="0" data-ischild="0" style=" background-color: #425fab;width:80px; padding:10px"><span>Send</span></a>
</div>
</div>
</form>和js
$(".add-comment").click(function () {
//some thing happens in here
});这个很好用。并且我可以访问.add-Comm.click()函数,但是在某些部分,我必须通过javascript添加文本区域,如下所示
let divStr = "<div class='w-75 d-inline-flex'><textarea id='ChildComment' class='reply-comment w-100' ></textarea></div>";
divStr = divStr + "<div class='w-25 d-inline-flex align-bottom' style='float:left; justify-content:left;'>";
divStr = divStr + "<a class='btn btn-buy d-inline add-comment' data-id='" + commentId +"' data-ischild='1'><span class='mr-1 ml-1'> Send</span></a></div>";
$("#" + commentId).append(divStr);但是,当我通过javascript创建textarea时,I不能输入.add-注释. can (函数())。
发布于 2022-04-22 08:39:08
试着改变
$(".add-comment").click(function () {
//some thing happens in here
});进入到这个
$(document).on('click', '.add-comment', function() {
//some thing happens in here
});第一个不能工作,因为项目是在加载DOM之后添加的。
发布于 2022-04-22 08:41:27
这是因为您试图将click事件附加到已动态添加的元素上。试试这个:
$(document).on('click','.reply-comment',function(){//do something})https://stackoverflow.com/questions/71965734
复制相似问题