【发布时间】:2011-10-07 09:14:56
【问题描述】:
我正在尝试弄清楚如何使以下表单中的逻辑起作用。基本上,如果选中前两个单选按钮中的任何一个,则使名为类别的隐藏输入具有蔬菜值。否则,让隐藏的输入命名为 categories 的值为 fruits。
我不确定这是否应该使用 PHP 或 JavaScript 完成,但如果使用 PHP 完成,我认为表单必须提交给自身进行预处理,然后收集、预处理信息将被发送到 external_form_processor.php。如果您是这样做的,我需要使用什么 PHP 代码来使其工作?
<?php
if($_POST["food"]=="carrots" || $_POST["food"]=="peas") {
$category = "vegetables";
} else {
$category = "fruits";
}
?>
<form name="food_form" method="post" action="external_form_processor.php" >
<fieldset>
<input type="radio" id="carrots" name="food" value="carrots" />
<input type="radio" id="peas" name="food" value="peas" />
<input type="radio" id="orange" name="food" value="orange" />
<input type="radio" id="apple" name="food" value="apple" />
<input type="radio" id="cherry" name="food" value="cherry" />
</fieldset>
<input type="hidden" name="categories" value="<?php $category ?>" />
</form>
如果使用jQuery会更容易,如果我在页面头部使用以下内容,我怎么能将变量称为隐藏输入的值?
$(function(){
$('input[name=food]').click(function(){
var selected_id = $('input[name=food]:checked').attr('id');
if (selected_id == 'carrots' || selected_id == 'peas') {
var category = "vegetables";
} else {
var category = "fruits";
}
});
});
对此的任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: php jquery forms conditional radio