问题
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.
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.
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.
回答1:
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.
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".
You'll need to parse your values out on the PHP side - e.g.
$colorArray = explode(' ',$_POST['question_9'])
The PHP explode() will break a string into an array using a delimiter, in this example using a space.
EDIT:
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"/>
来源:https://stackoverflow.com/questions/15075262/check-hidden-input-fields-based-on-radio-button-selected