0

所有,我有以下 PHP 代码:

$qry = "Select * from uploaded_files where user_id='$_SESSION[oml_user_id]' and upload_type='video_montage' order by sort ASC";
$result = mysql_query($qry);
$resultrows = mysql_num_rows($result);
if($resultrows>0){
?>
<ul class="gallery" id="video_gallery">
<?php
while($resultset = mysql_fetch_array($result)){
?>
<li id="<?php echo $resultset['file_id']; ?>"><a href="../upload/<?php echo $resultset['filename']; ?>"><img src="../upload/thumbnails/<?php echo $resultset['filename']; ?>" width="116" height="116" alt=""></a></li>
<?php
}
?>
</ul>
<button id="save_info">Save</button>

我想要发生的是当我单击 save_info 按钮时,我想确定 li 的当前顺序以及与 ul 关联的它们的相应 ID。然后我希望能够只使用 .post 函数将它们提交给一个函数,以将这些值保存到我的数据库中。

如何确定我页面上 lis 的当前顺序和 ID?

提前致谢!

4

1 回答 1

3

Well$("#video_gallery li")会给你一个 jQuery 对象,其中包含当前顺序中的所有 li 元素。然后,您可以遍历它们.each()并提取 id:

$("#save_info").click(function() {    
    var ids = [];
    $("#video_gallery li").each(function() {
        ids.push(this.id);
    });

    // do something with the ids, for example:
    $.post("test.php", { 'file_ids[]': ids });   
});

演示:http: //jsfiddle.net/5f4kr/

(或者,您可以使用各种其他 jQuery 方法,例如 ,$.map()但上述方法很简单。)

于 2012-07-10T04:19:39.230 回答