多个无线电领域和jQuery AJAX POST

时间:2014-08-21 09:36:49

标签: javascript jquery ajax html-form radio-group

我有一个用户上传的文件列表。网站管理员使用此列表批准/拒绝已上载到适合家庭的Web应用程序的用户头像上传。在表的左栏中我们有头像,在右栏中我们每行有一个唯一的无线电组,有三个选项:

<label><input type="radio" name="<?php echo $uploadId ?>" value="ignore" checked="" /> Ignore</label>
<label><input type="radio" name="<?php echo $uploadId ?>" value="approve" /> Approve</label>
<label><input type="radio" name="<?php echo $uploadId ?>" value="deny" /> Deny</label>

使用jQuery AJAX帖子提交表单:

$.ajax({
    type: 'POST',
    url: 'avatar-decision.php',
    data: "MY-PROBLEM-IS-THIS-BIT",
    dataType: 'json',
    success: function(htmldata) {
        if (htmldata.result === "success") {
             $('#requestMsg').html(htmldata.msg);
             $('#requestListing').get("avatar-listing.php");
        }  else {
             $('#requestMsg').html(htmldata.msg);
        }
}}); 

我想设置上面data字段的值,以捕获提交的广播组的所有名称和值。例如,如果我有5个名称为1,2,3,4,5的无线电组,我想捕获每个无线电设备的值,并将它们设置在我的ajax帖子的数据值中,如查询字符串: / p>

1=approved&2=ignore&3=deny&4=approve&5=approve

遵循以下结构:

[radio-group-name]=[radio-group-value]

这意味着我可以在request-listing.php中收集它们,循环遍历它们并根据它们的值对它们进行操作。

非常感谢, 仲裁器

2 个答案:

答案 0 :(得分:0)

一个简短的回答,但我认为这正是你所需要的。

var array = [];
$("input:radio").each(function(){
    if($(this).is(':checked')){
      array[$(this).attr('name')] = 'approved';
    }else{
      array[$(this).attr('name')] = 'discard';
    }                                                               
});

答案 1 :(得分:0)

所有归功于@hindmost,我在下面的评论中为我回答了此问题。

解决方案只是一个内线:

$.ajax({
    type: 'POST',
    url: 'pages/licence-manager/request-decision.php',
    data: $('#requestForm').serialize(),
    dataType: 'json',
    success: function(htmldata) {
        if (htmldata.result === "success") {
            $('#requestMsg').html(htmldata.msg);
            $('#requestListing').get("pages/licence-manager/request-listing.php");
        }  else {
            $('#requestMsg').html(htmldata.msg);
        }
    }
});  

查看数据值:

$('#avatarForm').serialize();

这一行解决了整个问题。

谢谢! 仲裁器