所以我有一个单选按钮组,如下所示:
<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可以作为一个选择?
发布于 2011-04-02 04:38:18
你可以在提交后这样做,如果你保持原来的命名约定,下面的代码应该可以工作。
$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-02 04:39:03
我想你说的是你正在提交的表格吧?
如果您想对发布的内容添加更精细的控制,您可以执行以下两项操作之一:
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-02 04:39:45
如果可以,请避免客户端的详细说明。
否则,请使用.submit()并手动编写所需的代码。但这更难。
https://stackoverflow.com/questions/5518444
复制相似问题