Javascript 通过ajax提交表单时新添加的复选框出错
在不刷新页面的情况下,无法提交另一个ajax脚本添加了新复选框的删除ajax脚本。就像serialize没有看到它们一样Javascript 通过ajax提交表单时新添加的复选框出错,javascript,jquery,ajax,Javascript,Jquery,Ajax,在不刷新页面的情况下,无法提交另一个ajax脚本添加了新复选框的删除ajax脚本。就像serialize没有看到它们一样 $('input:submit.delete-checked-categories').on('click', function() { $.ajax({ type: 'POST', url: 'delete-selected-categories',
$('input:submit.delete-checked-categories').on('click', function()
{
$.ajax({
type: 'POST',
url: 'delete-selected-categories',
data: $('form.categories').serialize(),
dataType: 'json',
success: function(data) {
$('div#alert').html('Done!');
$('div#alert').animate({top: '100'}, 'fast');
$('div#alert').fadeIn(300);
$('div#alert').delay(200).fadeOut(300);
$("input[type=checkbox]:checked").closest("tr").remove();
},
error: function(data) {
$('div#alert').html('Error!');
$('div#alert').animate({top: '100'}, 'fast');
$('div#alert').fadeIn(300);
$('div#alert').delay(200).fadeOut(300);
}
});
});
这就是我的提交输入jquery脚本的样子
我可以选中许多复选框,按下它,然后从表中删除数据库中的所有行。但是我可以通过下面的脚本,在这个表单中添加带有新输入的新行
$('input[name="new-category"]').on('click', function(form) {
form.preventDefault();
$.ajax({
type: 'POST',
url: 'add-category-in-categories',
data: $('form.categories').serialize(),
dataType: 'json',
success: function(data)
{
$('div#alert').html('Done!');
$('div#alert').animate({top: '100'}, 'fast');
$('div#alert').fadeIn(300);
$('div#alert').delay(200).fadeOut(300);
$('table.table1 > tbody:last-child').append(data);
},
error: function(data)
{
$('div#alert').html('Error!');
$('div#alert').animate({top: '100'}, 'fast');
$('div#alert').fadeIn(300);
$('div#alert').delay(200).fadeOut(300);
}
});
});
桌子看起来像
...
<tr id="row-32">
<td>
<input type="checkbox" name="category[]" value="32" />
</td>
<td>
<a href="http://localhost/projekt2/admin/post?id=32">11</a> (0) </td>
<td>
111 </td>
</tr>
<tr id="row-33">
<td>
<input type="checkbox" name="category[]" value="33" />
</td>
<td>
<a href="http://localhost/projekt2/admin/post?id=33">222</a> (0) </td>
<td>
222 </td>
</tr>
<tr id="row-11">
<td>
<input type="checkbox" name="category[]" value="11" />
</td>
<td>
<a href="http://localhost/projekt2/admin/post?id=11">dfgdfgsfdgdfg</a> (1) </td>
<td>
sdgfgdgfdgfdgfdgdfgdfg </td>
</tr>
...
在没有ajax和jquery的情况下,只在这个“category[]”的var_dump上执行form post请求,只检查新添加的category,我得到了“NULL”值
此输入在表中。整张桌子都在里面。效果很好,但必须刷新页面
php脚本:
echo json_encode('<tr>
<td>
'.form_checkbox(array('name' => 'category[]', 'value' => $category['id'], 'checked' => 'checked')).'
</td>
<td>
'.anchor('admin/category?id='.$category['id'], $category['nazwa']).'
</td>
<td>
'.$category['opis'].'
</td>
</tr>');
echo json_encode('
“.form_复选框(数组('name'=>'category[],'value'=>$category['id'],'checked'=>'checked'))。”
“.anchor('admin/category?id=”.$category['id'],$category['nazwa'])。”
“.$category['opis']”
');
我猜想您已成功地将动态添加的复选框放置在表单之外。。。但是我们没有看到表格或表格,所以很难说。它们在表格内。整个表的格式是@CBroeWell,这将是我能想到的“serialize没有看到它们”的唯一可能解释。但是对于您所展示的代码片段,很难说清楚。你能创建一个脚本吗?好的,我在你的脚本中发现了一个bug。当您发送请求获取新行时,您将数据类型设置为json,但无法将json格式的数据附加到html,这就是发生错误的原因…@TohidDadashnezhad我不认为是这样,因为它附加了好行,这是好的格式化行,除了ajax之外,它甚至不适用于标准格式的post请求-类别[]中的NULL我的猜测是,您成功地将动态添加的复选框放置在表单之外。。。但是我们没有看到表格或表格,所以很难说。它们在表格内。整个表的格式是@CBroeWell,这将是我能想到的“serialize没有看到它们”的唯一可能解释。但是对于您所展示的代码片段,很难说清楚。你能创建一个脚本吗?好的,我在你的脚本中发现了一个bug。当您发送请求获取新行时,您将数据类型设置为json,但无法将json格式的数据附加到html,这就是发生错误的原因…@TohidDadashnezhad我不认为是这样,因为它附加了好行,这是好的格式化行,除了ajax之外,它甚至不适用于标准格式的post请求-类别[]中的NULL
echo json_encode('<tr>
<td>
'.form_checkbox(array('name' => 'category[]', 'value' => $category['id'], 'checked' => 'checked')).'
</td>
<td>
'.anchor('admin/category?id='.$category['id'], $category['nazwa']).'
</td>
<td>
'.$category['opis'].'
</td>
</tr>');