当前位置: 动力学知识库 > 问答 > 编程问答 >

php - jQuery: Radio button selection determens which div to display, with ”checked” attribute

问题描述:

I have a form with different options provided by radio buttons. I also have a script which displays different divs with different form inputs.

This script works fine, my problem is that I have a “previous” link where a user can go one step back and edit already entered data and that I can’t force the different options with the checked attribute to the radio button. It has to be clicked, which make sense because my script uses the .click event.

But how can I say something like: “If $_SESSION[‘unemployed’] == ‘yes’ then echo ‘checked’” and it shows the right option.

I hope I explained myself correctly otherwise fell free to ask.

Please see the code live here http://jsfiddle.net/GEFMX/1/ and my question is if one or more or the checkboxes default is "checked".

The Javascript I’m using looks like this:

<script type="text/javascript">

$(function() {

$("input[name$='step2_student']").click(function() {

var diffoptions = $(this).val();

$("div.studerende_janej").hide();

$("#studerende" + diffoptions).show();

});

$("input[name$='step2_otheredu']").click(function() {

var diffoptions = $(this).val();

$("div.othereducation_janej").hide();

$("#othereducation" + diffoptions).show();

});

$("input[name$='step2_haveeducation']").click(function() {

var diffoptions = $(this).val();

$("div.haveeducation_janej").hide();

$("#haveeducation" + diffoptions).show();

});

});

</script>

Sincere

- Mestika

网友答案:

You can add some code to your page load to trigger a 'click' for all radios that are checked. i.e. something like:

$(function() {

// .. bind your click events here

$('input[type="radio"]').each(function() {
    if($(this).prop('checked')) $(this).click(); // trigger 'click' if checked
});

});
网友答案:

At the end of your document ready, add $("input[name~='step2_']:checked").click(). This will trigger the click event for already checked radio buttons, effectively displaying the divs.

分享给朋友:
您可能感兴趣的文章:
随机阅读: