【发布时间】:2015-08-29 22:31:34
【问题描述】:
我使用这个 jQuery 函数向我的页面添加和删除框时遇到问题:
jQuery(document).ready(function($){
$('.my2-form .add2-box').click(function(){
var n = $('.text2-box').length + 1;
if( 12 < n ) {
alert('you can't make more than 12 box');
return false;
}
$.post('showselectdatearray.php', { type: 'months', year: 93}, function(result) {
var box_html = $('<p class="text2-box" style="padding: 0px; margin: 0px;"><label for="box' + n + '"><span class="box2-number">' + n + ' </span></label> <input type="text" name="boxes[]" value="" id="box' + n + '" size="8" /> '+result<a href="#" class="remove2-box">removeitem</a></p>');
box_html.hide();
$('.my2-form p.text2-box:last').after(box_html);
box_html.fadeIn('slow');
box_html.css( 'background-color', '#48b973' );
return false; });
});
$('.my2-form').on('click', '.remove2-box', function(){
$(this).parent().css( 'background-color', '#FF6C6C' );
$(this).parent().fadeOut("slow", function() {
$(this).remove();
$('.box2-number').each(function(index){
var p =index+1;
var str = p+'\xa0\xa0\xa0\xa0';
$(this).text( str );
});
});
return false;
});
$('.my2-form p.text2-box:last').css( 'background-color', '#FFFFFF' );
});
然后我以这种方式在我的代码中使用上述脚本:
<div id="showresult12" class="my2-form">
<div>
<input type='button' id='AddMore' name='AddMore' value='add box' class='add2-box' />
</div>
<div style="float: right;" class="scroll10">
<p class="text2-box" style="padding: 0px; margin: 0px;">
<label for="box"><span class="box2-number">1 </span></label>
<input type="text" name="boxes[]" value="" id="box" size="8" />
<?php Show_Select_Date_Array("months",0,0,0,93) ?>
</p>
</div>
</div>
我在此代码下还有另一部分 -part2- 从数据库中获取。用户单击位于-part2- 中的“编辑”按钮后,信息从数据库获取到带有 php 代码的框,但我的添加和删除按钮根本不起作用。我的信息使用 Ajax 获取并替换在新框中。 'showresult12' div id 再次使用相同的数据完全加载。
更换div元素后有什么问题?!
我必须在我的 jquery 代码中做哪些更改才能在再次加载 div 后工作?
【问题讨论】:
标签: document-ready