jQuery-将(项)附加到最近的类
--更新-- 来自jQuery-将(项)附加到最近的类,jquery,ajax,Jquery,Ajax,--更新-- 来自url:'ajax\u post\u com.php' echo "<div class='notat' id='typenote".$clients_id."'> <form class='formet' method='post'> <textarea name='note' id='note' placeholder='Legg til notat'></textarea>
url:'ajax\u post\u com.php'
echo "<div class='notat' id='typenote".$clients_id."'>
<form class='formet' method='post'>
<textarea name='note' id='note' placeholder='Legg til notat'></textarea>
<input type='text' name='client_id' id='client_id' value='".$clients_id."' style='display:none;' />
<input type='text' name='user' id='user' value='".$class->getCurrentUser('users_fullname')."' style='display:none;'/>
<input type='submit' value='SAVE' id='Up'/>
<input type='button' class='page-fade' value='CANCEL' id='Down'/>
</form>
</div>";
?>
<div style="clear:both; height:2px;"></div>
<?php
$note_query = mysql_query(
"SELECT * FROM ... WHERE client_id = $clients_id ORDER BY note_id DESC");
?>
<div class="note-block"></div>
<?php while($nota = mysql_fetch_array($note_query)): ?>
<div class="note_post">
<strong><img src='img/user.png' width='15px'> <?php echo $nota['user']?></strong>
<?php echo strftime('%e', strtotime($nota['note_created']));?>.
<?php echo strftime('%b', strtotime($nota['note_created']));?>.
<?php echo strftime('%y', strtotime($nota['note_created']));?> (<?php echo date('H:i',strtotime($nota['note_created']))?>)
<br>
<p><?php echo $nota['note']?></p>
</div>
<?php end while?>
(吉隆坡)
尝试添加表单作为调用的上下文,并使用它查找div
$(this).next('.note-block').append(item);
根据您提供的HTML,它看起来像
。注意block
是表单的父项的同级。因此,请尝试:
$.ajax({
url: 'ajax_post_com.php',
type: 'POST',
cache: false,
data: $(this).serialize(), //$(this) = a form
beforeSend: function(){
submit.val('Submitting...').attr('disabled', 'disabled');
},
context: this,
success: function(data){
var item = $(data).hide().fadeIn(800);
$(this).parent().nextAll('.note-block:first').append(item);
//GOAL: append(item) onto the .note-block after only (this) form
},
编辑(再次-基于OP的新代码)
在调用ajaxsuccess
处理程序时,$(this)
可能没有引用原始表单元素。您可以在提交处理程序中保存对表单(正在提交)的引用,以便在$调用中使用。ajax
调用:
$(this).parent().nextAll('.note-block').append(item);
您甚至可以保存对目标的引用。注意,要更新的block
:
$(document).ready(function() {
var allForms = $('.formet');
var submit = $('#knappUp');
allForms.on('submit', function(e) {
e.preventDefault();
//form variable created inside submit handler
var thisForm = $(this);
$.ajax({
url: 'ajax_post_com.php',
type: 'POST',
cache: false,
//use the form variable
data: thisForm.serialize(),
beforeSend: function(){
submit.val('Submitting...').attr('disabled', 'disabled');
},
success: function(data){
var item = $(data).hide().fadeIn(800);
//use the form variable
thisForm.parent().nextAll('.note-block:first').append(item);
}
});
});
});
你能发布一个HTML的小片段吗。感谢您的“heeling”,在成功处理程序
$(this)
中不太可能仍然引用正在提交的表单。是否在另一个函数的上下文中调用$.ajax
?否,仅此函数。好的……是否确定$(此)
是表单元素?您可以登录到控制台并验证吗?从原始帖子:“data:$(this).serialize(),”这很有效。获取表单中的输入数据-所有数据都已成功发送到Table是的,请参阅我对原始帖子的附加评论$(this)
在调用success
处理程序时可能没有引用表单。那么,您建议改为什么?有什么想法吗?好的!听起来不错。但是缺少了一些东西。。。尝试过之后,帖子将提交到所有class=note块。还尝试了.next()-相同的结果
$(this).parent().nextAll('.note-block').append(item);
$(document).ready(function() {
var allForms = $('.formet');
var submit = $('#knappUp');
allForms.on('submit', function(e) {
e.preventDefault();
//form variable created inside submit handler
var thisForm = $(this);
$.ajax({
url: 'ajax_post_com.php',
type: 'POST',
cache: false,
//use the form variable
data: thisForm.serialize(),
beforeSend: function(){
submit.val('Submitting...').attr('disabled', 'disabled');
},
success: function(data){
var item = $(data).hide().fadeIn(800);
//use the form variable
thisForm.parent().nextAll('.note-block:first').append(item);
}
});
});
});
allForms.on('submit', function(e) {
var thisForm = $(this);
var noteBlock = thisForm.parent().nextAll('.note-block:first');
//try console.log(noteBlock); right here to see what it is
$.ajax({
...
success: function(data){
var item = $(data).hide().fadeIn(800);
//use the noteBlock variable
noteBlock.append(item);
}
});
});