문제

i have the following page:

<html>
<body>
    <form>
        <input type="hidden" id="tab_indexer" />
        <input type="checkbox" id="tab_id" />
        <input type="checkbox" id="tab_message" />
    </form>
</body>
</html>

So, what i need is to, as the user changes the checked checkbox, the value on the hidden field changes as well. Also, when the page loads, it must set the checkbox referent on the hidden field as checked.

도움이 되었습니까?

해결책

I think this might hep you. http://jsfiddle.net/Ckv5w/

Here I am using the radio group for the purpose. I am changing the value of your hidden field according to the radio button selected and displaying it in the div below. You can use the same value where ever you want.

HTML

<form>
    <input type="hidden" id="tab_indexer" value="" />
    <lable>
        <input type="radio" name="group1" id="tab_id" />
        One
    </lable><br/>
    <lable>
        <input type="radio" name="group1" id="tab_message" />
        Two
    </lable>
</form>
<div class="display"><p></p></div>

jQuery

$(document).ready(function(){
$("input[type=radio]").on('click', function(){
    $("input[type=hidden]").attr("value", $(this).attr("id"));
    $(".display p").text($("input[type=hidden]").val());
});
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top