سؤال

I have a form with lots of inputs. I am using jQuery so if a person selects the answer "Yes" a text box appears if they click "no" nothing appears. My problem is when a person submits the page and the page runs through the PHP validation if it throws the page (incurs reload) back with an error while I can get the Radio button to remember the value chosen (using PHP) it will not re-show the textbox if the user clicked yes.

Basically: User Picks Yes, text box shows. User submits page, page thrown back due to validation error. Radio button remembers state "Yes" but text box is not shown unless re clicking yes.

Any idea how I can get the textbox to show up?

jQuery

$("input[name='radioButton']").change(function(){
    if ($(this).val() == "Yes")
    {
        $("#txt1").show();
    }
    else
    {
        $("#txt1").hide(); 
    }
});
هل كانت مفيدة؟

المحلول

You can look to the checked radio button on page load:

$(document).ready(function(){
    if($("input[name='radioButton'][value='Yes']").is(':checked'))
        $("#txt1").show();
    else
        $("#txt1").hide(); 
});

EDIT: Changed to use ".is(':checked')"

نصائح أخرى

EDIT: This is the correct way to do it. From Ryan Henderson's answer:

$(document).ready(function(){
    if($("input[name='radioButton'][value='Yes']").is(':checked'))
        $("#txt1").show();
    else
        $("#txt1").hide(); 
});

It's not supposed to replace your current change event handler. It basically checks the value of the radio button when the page is loaded.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top