<select name="securityq1" class="selectInput">
<option value="">Please select</option>
<option value="What is your matric no?">What is your matric no?</option>
<option value="What is the name of your lover?" >What is the name of your lover?</option>
<option value="What is your profession?" >What is 开发者_高级运维your profession?</option>
</select>
Hi all, this is my security question, when I want to do the update function, I want to draw from the database the question users have previously entered. If questions are little, I can do stupidly <option value="What is your matric no?" <?php if($database['securityq1']=="What is your matric no") echo "selected"; ?>>...</option>
But the security questions may up to 15 questions, do this way is very tedious? As I am using jQuery for UI , how can I make a quick way using jQuery? Thanks for help
You have to determine the selected value somehow on the server-side. I'd suggest that you keep the options in an array or some other collection and loop over them:
$options = array("What is your matric no?", ...);
foreach($options as $option) {
echo "<option value=\"$option\"";
if($database['securityq1'] === $option) {
echo ' selected';
}
echo ">$option</option>";
}
You can wrap the routine in a function and handle all similar situations easier.
If you insist using jQuery you can do something like this
$('select[name=securityq1] option[value="'+ selectedSecurityQuestion +'"]').attr('selected','selected');
Where selectedSecurityQuestion
is defined somewhere. See it in action.
However,this solution won't work if javascript is turned off. Whether that is a problem is determined by the target audience.
try with this once ..
<script>
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
});
$("div").text(str);
})
.trigger('change');
</script>
精彩评论