0

所以我有一个单选按钮组,如下所示:

<div data-role="fieldcontain">
    <fieldset data-role="controlgroup">
        <legend>Choose a pet:</legend>
            <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
            <label for="radio-choice-1">Cat</label>

            <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2"  />
            <label for="radio-choice-2">Dog</label>

            <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3"  />
            <label for="radio-choice-3">Hamster</label>

            <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4"  />
            <label for="radio-choice-4">Lizard</label>
    </fieldset>
</div>

现在,如果我只是提交我得到这个 $_POST (注意我有多个广播组问题)

Array
(
    [radio-choice-1] => choice-1
    [radio-choice-2] => choice-4
    [radio-choice-3] => choice-2
    [submit] => submit
    [PHPSESSID] => 11111111111111111
)

如何在提交之前重组 HTML 或 $_POST 数据,使其看起来像这样:

Array
(
    [type-1] => radio-choice-1
    [answer-1] => choice-1
    [type-2] => radio-choice-2
    [answer-2] => choice-4
    [type-3] => radio-choice-3
    [answer-3] => choice-2
    [submit] => submit
    [PHPSESSID] => 11111111111111111
)

也许 jQuery 作为一个选项?

4

3 回答 3

3

您可以在提交后进行,如果您保持原来的命名约定,下面的代码应该可以工作。

$postValues = $_POST;
$altered = Array();
$unaltered = Array();

foreach ($postValues as $key => $val) {

  if ( FALSE !== stripos($key, 'radio-choice-') ) {

    $num = explode('-', $key);
    $num = $num[2];
    $altered['type-'.$num] = $key;
    $altered['answer-'.$num] = $value;

  } else {

    $unAltered[$key] = $value;

  }  

}

$manipulatedPOSTData = array_merge($altered, $unAltered);

// Keep doing what you intended
于 2011-04-01T20:38:18.193 回答
1

我假设您正在谈论您正在提交的表格?

如果您想更好地控制发布的内容,您可以执行以下两项操作之一:

1)添加隐藏变量

或者

2) 使用 jQuery .post() (http://api.jquery.com/jQuery.post/) 而不是进行正常的表单提交。

就个人而言,我认为两者中的第一个是最简单的:

<div data-role="fieldcontain">
    <input type="hidden" name="type-1" value="radio-choice-1" />
    <fieldset data-role="controlgroup">
        <legend>Choose a pet:</legend>
            <input type="radio" name="answer-1" id="radio-choice-1" value="choice-1" checked="checked" />
            <label for="radio-choice-1">Cat</label>

            <input type="radio" name="answer-1" id="radio-choice-2" value="choice-2"  />
            <label for="radio-choice-2">Dog</label>

            <input type="radio" name="answer-1" id="radio-choice-3" value="choice-3"  />
            <label for="radio-choice-3">Hamster</label>

            <input type="radio" name="answer-1" id="radio-choice-4" value="choice-4"  />
            <label for="radio-choice-4">Lizard</label>
    </fieldset>
</div>

通过将无线电地面的名称更改为 answer-1 并添加一个隐藏变量,这应该满足您的要求(对您的其他无线电元素执行相同的操作)。

于 2011-04-01T20:39:02.927 回答
0

如果可以,请避免客户端详细说明。

否则使用.submit()并手动编写您需要的代码。但更难。

于 2011-04-01T20:39:44.917 回答