根据选择的单选按钮检查隐藏的输入字段 [英] Check Hidden Input Fields Based On Radio Button Selected

查看:109
本文介绍了根据选择的单选按钮检查隐藏的输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里进行了搜索,但是尽管发现了很多类似且很可能更为复杂的情况,但仍找不到我想要做的事情的答案.

I HAVE searched on here but, despite finding LOTS of similar and most likely MUCH more complicated situations, couldn't find an answer to what I am wanting to do.

基本上,我想在我网站的搜索表中显示2个单选按钮选项:

Basically, on my site's search form I want to present 2 radio button options:

<label><input type="radio" name="question_9[]" value="Blue" checked="checked" />Blue</label>    
<label><input type="radio" name="question_9[]" value="Green" />Green</label>

我想用搜索表格提交的是:

BUT what I want to be submitted with the search form is:

<input type="hidden" name="question_9[]" value="Blue"  checked="checked"/>
<input type="hidden" name="question_9[]" value="Blue/Green"  checked="checked"/>

OR

<input type="hidden" name="question_9[]" value="Green"  checked="checked"/>
<input type="hidden" name="question_9[]" value="Blue/Green"  checked="checked"/>

取决于提交表单时选中了哪个单选按钮.

depending on which radio button is checked when the form is submitted.

我正在使用的软件最近从scriptaculous迁移到了JQuery,这是我的新手.我希望有一种基于选择哪个单选按钮为问题传递2个值的简便方法,因为据我所知,除非我采取这种变通方法,否则我只能通过单选按钮选择一个值.由于单选按钮本身不会/不应该使用表单提交值,因此可以重命名它们或进行任何必要的操作以使其起作用.

The software I am using recently migrated from scriptaculous to JQuery, which I am completely new to. I am hoping there is an easy way to pass 2 values for the question based on which radio button is selected, since as far as I know I can only select ONE value with radio buttons unless I do a workaround like this. Since the radio buttons themselves won't/shouldn't be submitting values with the form, they can be renamed or whatever needs be done to make it work.

我真的很感谢任何提示,我再次发现并没有发现另一个问题要求这样做(令我感到惊讶),但是希望我对这样的问题并没有多余.

I really appreciate any tips and again, I looked and didn't find another question asking to do the same as this (kind of surprised me) but HOPE that I am not being redundant with a question like this.

推荐答案

无论使用单选按钮,复选框,隐藏文本字段还是其他内容,您都无法提交两个具有相同名称的值.您需要考虑处理数据的PHP/ASP脚本会看到什么-例如对于PHP,$ _ POST ['question_9'](或$ _GET ['question_9'])应具有什么值?该值需要从单个字段传递.

You won't be able to submit two values with the same name - whether you use radio buttons, checkboxes, a hidden text field, or whatever. You need to think about what the PHP/ASP script processing your data will see - e.g. for PHP, what value should $_POST['question_9'] (or $_GET['question_9']) have? That value needs to be passed from a single field.

好消息是,您不需要任何精美的javascript技巧即可将正确的信息传递给脚本.如果您希望PHP变量为蓝色蓝色/绿色",则只需将单选按钮的值"元素更改为蓝色蓝色/绿色"即可.

The good news is that you don't need any fancy javascript tricks to pass the right info to your script. If you want your PHP variable to be "Blue Blue/Green" you can just change the 'value' element of your radio button to "Blue Blue/Green".

您需要在PHP方面解析您的值-例如

You'll need to parse your values out on the PHP side - e.g.

$colorArray = explode(' ',$_POST['question_9'])

PHP explode()将使用定界符,在此示例中使用空格.

The PHP explode() will break a string into an array using a delimiter, in this example using a space.

为澄清起见,解决方案更简单.单选按钮将使您知道是否提交了蓝色或绿色-如果您还希望同时将蓝色/绿色作为"question_9"数组的一部分提交,则无论是否选中了蓝色或绿色,您都应该只添加一个即可如您所说的隐藏字段(删除选中"属性,因为它仅适用于复选框和单选按钮):

In response to the clarification, the solution is simpler. The radio button will get you whether Blue or Green is submitted - if you want to also submit Blue/Green with it as part of the "question_9" array regardless of whether Blue or Green is checked, you should be able to just add a single hidden field like you said (removing 'checked' attribute since it only applies to checkboxes and radio buttons):

<input type="hidden" name="question_9[]" value="Blue/Green"/>

这篇关于根据选择的单选按钮检查隐藏的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆