如何在页面加载时选中单选按钮时隐藏div?

时间:2016-01-16 15:27:03

标签: javascript jquery html css radio-button

我有一个现有的jquery代码,如果选中了单选按钮,它会隐藏div。但是,当页面加载时...如果已经选中了这个单选按钮,我希望能够隐藏div而不点击任何内容。

<script>
    $('input[name=resident]').click(function () {
            if (this.id == "residentNo") {
                $(".d").hide('slow');
                $('.d').find('#precinctNum').val('');
                $('.d').find('#yearStartedLiving').val('');                 
            } else {
                $(".d").show('slow');
                $('.d').find('#precinctNum').val('');
                $('.d').find('#yearStartedLiving').val('');
            }
        });     
</script>

html表格:

<label class="radio-inline"><input type="radio" class = "res" id = "residentYes" name="resident" value="Yes" checked> Yes</label>
<label class="radio-inline"><input type="radio" class = "res" id = "residentNo"  name="resident" value="No" > No</label>

<div class="d item form-group" >
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Precinct Number <span class="required">*</span>
    </label>
<div class="col-md-6 col-sm-6 col-xs-12">
    <input id="precinctNum" class="form-control col-md-5 col-xs-12" name="precinctNum" data-validate-length-range="7" required="required" type="number">
</div>
</div>

<div class="d item form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Start of Residency (Year) <span class="required">*</span>
    </label>
<div class="col-md-6 col-sm-6 col-xs-12">
    <input type="text" id="yearStartedLiving" name="yearStartedLiving" class="form-control col-md-5 col-xs-12" required="required" data-inputmask="'mask': '9999'">
</div>
</div>

我已经尝试了好几个小时,但它似乎无法奏效。非常感谢您的帮助!!谢谢!

3 个答案:

答案 0 :(得分:1)

你可以像下面这样做。只需在$validator ->add('pseudonym','unique',['rule' => 'validateUnique','provider' => 'table']); 中添加代码即可。希望它有所帮助。

$(document).ready
if($('#residentNo').is(':checked')){
    $(".d").hide();
    $('.d').find('#precinctNum, #yearStartedLiving').val('');
}

$('input[name=resident]').click(function () {
     if (this.id == "residentNo") {
         $(".d").hide('slow');
         $('.d').find('#precinctNum, #yearStartedLiving').val('');               
     } else {
         $(".d").show('slow');
         $('.d').find('#precinctNum, #yearStartedLiving').val('');
     }
});

答案 1 :(得分:1)

 document.addEventListener("DOMContentLoaded", function(event) { // Will check after DOM is ready
        if($('#residentNo').is(':checked')){  // Check if residentNo is checked
        toggleDiv("residentNo")   
        }
      });


    $('input[name=resident]').click(function () {
    toggleDiv(this.id)

    })

    function toggleDiv(id){
                if (id == "residentNo") {
                    $(".d").hide('slow');
$(".d").find('#precinctNum, #yearStartedLiving, #birthplace, #weight, #height, #income').val('');

                } else {
                    $(".d").show('slow');
$(".d").find('#precinctNum, #yearStartedLiving, #birthplace, #weight, #height, #income').val('');

                }
            };

JSFIDDLE

注意:在此示例中,我已将residentNo设置为checked以进行演示

答案 2 :(得分:1)

您可以触发活动点击您感兴趣的字段:

$('input[name=resident][value="No"]').trigger('click');

$('#atp_13').append("<a data-remote="true" rel="nofollow" data-method="delete" href="/clots/test/unjoin"><i class='icon-check clot-unfollow-icon'></i></a>)");
$('#<%= "atp_#{@atp.id}" %>').append("<%= link_to unjoin_atp_path(@atp), :method => :delete, remote: true do %><i class='icon-check clot-unfollow-icon'></i><% end %>)");