0
我試圖使用jQuery自動完成來顯示評論表單下的主持人列表,用戶可以選擇通知他們的評論。如何在克隆表單上使用jQuery自動完成多個
評論系統是嵌套的,當用戶點擊之前存在的評論的「回覆」時,主評論表單的克隆被附加到該評論。自動填充功能可以在主要評論表單上正常工作,但不能在任何複製表單上使用。這是代碼的簡化版本:
<form id="myForm" action="submitComment.php" method="post" onsubmit="event.preventDefault();" >
<table>
<tr>
<th class="small"> Username </th>
<th colspan="4">
<span>New Post</span><br/><br/>
<input name="subject" class="small" value="" placeholder="Optional subject (25 char max)." />
</th>
</tr>
<tr>
<td>User Avatar</td>
<td colspan="4" >
<strong>Body</strong>
<br/>
<br/>
<textarea name="body" rows="4" placeholder="Comment body." > </textarea>
<br/>
<br/>
<input name="alert" class="alert" value="" placeholder="Notify mod(s) (Optional) "/>
</td>
</tr>
<tr>
<td></td>
<td colspan="4" ><input id="submit" type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>
<div id="comments">
<div id="comment 1" class="comment dark">
<div class="comment-holder">
<div class="body">Comment 1</div>
<div class="meta">
USERNAME DATE <span class="reply"> REPLY</span>
</div>
</div>
</div>
<div id="comment 2" class="comment dark">
<div class="comment-holder">
<div class="body">Comment 2</div>
<div class="meta">
USERNAME DATE <span class="reply"> REPLY</span>
</div>
</div>
</div>
<div class="spacer" style="clear: both;"></div>
</div>
<script>
//Append Level 1 comment reply form
$(document).on('click','.reply',function(e){
if (e.target !== this)
return;
$(this).addClass("active");
var form = $('#myForm').clone(true, true);
var target = $(e.target);
var isFormAvailable = $("#myForm", target).length > 0;
var level = 2;
if(!isFormAvailable) {
$(e.target).append(form);
$('textarea', form).focus();
$(form).addClass("child");
$(form).append("<span class=\"cancel\" style=\"color:red\">CANCEL</span>");
$(".cancel").click(function(){
$(this).parents(".reply").removeClass("active");
$(this).closest(form).remove();
});
}
});
$(function() {
var availableTags = [ "@user1", "@user2", "@user3"];
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("body")
// don't navigate away from the field on tab when selecting an item
.on("keydown click", ".alert", function(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 1,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
});
</script>
小提琴:https://jsfiddle.net/crashTestDummy/gbLz53re/