我有一個現有的jQuery代碼,如果單選按鈕被選中,它將隱藏div。但是,當頁面加載..如果這個單選按鈕已被選中,我希望能夠隱藏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>
我一直在嘗試了幾個小時,它似乎並沒有工作。您的幫助將非常感謝!謝謝!
這爲我工作!非常感謝!!最後,我的問題解決了,謝謝你!我的隊友也表示感謝。 –
@WandaMaximoff請考慮接受它作爲回答 – brk
完成。謝謝!!! –