如何发送一个HTML数组带来AJAX(How to send an HTML array trought AJAX)

编程入门 行业动态 更新时间:2024-10-12 03:19:49
如何发送一个HTML数组带来AJAX(How to send an HTML array trought AJAX)

我有这些复选框:

<input type="checkbox" name="style[]" value="1" checked="checked" /> <input type="checkbox" name="style[]" value="2" /> <input type="checkbox" name="style[]" value="3" checked="checked" /> <input type="checkbox" name="style[]" value="4" checked="checked" /> <input type="checkbox" name="style[]" value="5" />

和这个jQuery处理程序:

<script type="text/javascript"> $(document).ready(function() { $('.linkStyle').click(function(e) { // }); }); </script>

所以,当我点击一个复选框,我想发送数组样式[]到AJAX服务器(与实际值)。

我该怎么做?

编辑

试过:

$.post("list/browse_ajax.php", {id:"browseUpdate", actualArtist:browseArtist, actualEvent:browseEvent, actualData:browseData, style:$('#browseTableStyle:checkbox').serialize()}, function(data) { $("#browseList").html(data); } );

但样式[]不会发送...

编辑2 - 序列化的问题

这是整个代码:

<form id="browseForm"> <div class="wideinfo"> <div class="content"> <div class="tableList1"> Style </div> <div class="tableList2"> <span><input type="checkbox" name="style[]" value="Style1" checked="checked" /> Style1</span> <span><input type="checkbox" name="style[]" value="Style2" /> Style2</span> <span><input type="checkbox" name="style[]" value="Style3" checked="checked" /> Style3</span> <span><input type="checkbox" name="style[]" value="Style4" checked="checked" /> Style4</span> <span><input type="checkbox" name="style[]" value="Style2" /> Style5</span> </div> </div> </div> </form>

但是:

style:$('#browseForm').serialize()

它发送这个(在提琴手上检查)

style: style%5B%5D=Style1&style%5B%5D=Style3&style%5B%5D=Style4

不是:

style[]: Style1 style[]: Style3 style[]: Style4

最后一个是真正的/全面的HTML数组...

I have these checkboxes :

<input type="checkbox" name="style[]" value="1" checked="checked" /> <input type="checkbox" name="style[]" value="2" /> <input type="checkbox" name="style[]" value="3" checked="checked" /> <input type="checkbox" name="style[]" value="4" checked="checked" /> <input type="checkbox" name="style[]" value="5" />

and this jquery handler :

<script type="text/javascript"> $(document).ready(function() { $('.linkStyle').click(function(e) { // }); }); </script>

so, when I click on a checkbox, I'd like to send the array style[] to the server trought AJAX (with the actual values).

How can I do it?

EDIT

Tried :

$.post("list/browse_ajax.php", {id:"browseUpdate", actualArtist:browseArtist, actualEvent:browseEvent, actualData:browseData, style:$('#browseTableStyle:checkbox').serialize()}, function(data) { $("#browseList").html(data); } );

but the style[] is not send...

EDIT 2 - Problem with serialize

This is the whole code :

<form id="browseForm"> <div class="wideinfo"> <div class="content"> <div class="tableList1"> Style </div> <div class="tableList2"> <span><input type="checkbox" name="style[]" value="Style1" checked="checked" /> Style1</span> <span><input type="checkbox" name="style[]" value="Style2" /> Style2</span> <span><input type="checkbox" name="style[]" value="Style3" checked="checked" /> Style3</span> <span><input type="checkbox" name="style[]" value="Style4" checked="checked" /> Style4</span> <span><input type="checkbox" name="style[]" value="Style2" /> Style5</span> </div> </div> </div> </form>

but with :

style:$('#browseForm').serialize()

it sends this (checked on Fiddler)

style: style%5B%5D=Style1&style%5B%5D=Style3&style%5B%5D=Style4

not :

style[]: Style1 style[]: Style3 style[]: Style4

The last is a real/comprensive HTML array...

最满意答案

当你finnaly有你的字符串提交style%5B%5D=Style1&style%5B%5D=Style3&style%5B%5D=Style4你可以像这样检索全面的数组:

parse_str($_POST['style'], $data);

$data变量包含:

array( "style" => array( 0 => "Style1", 1 => "Style3", 2 => "Style4", ) )

它能解决你的问题吗?

When you finnaly have your string submitted style%5B%5D=Style1&style%5B%5D=Style3&style%5B%5D=Style4 you can retrieve the comprehensive array like that:

parse_str($_POST['style'], $data);

The the $data variable contains:

array( "style" => array( 0 => "Style1", 1 => "Style3", 2 => "Style4", ) )

Does it solve your problem ?

更多推荐

本文发布于:2023-08-01 11:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   AJAX   HTML   trought   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!