![](/img/trans.png)
[英]JavaScript: Check/Uncheck box depending on dropdown menu selection
[英]check box depending input fields cannot reset after uncheck
我創建了一個復選框及其2個相關的輸入字段,當我選中該復選框時,出現2個輸入字段,然后在其中輸入內容,但是當我取消選中該復選框時,輸入字段無法重置該值並提交。 當我取消選中然后根據其輸入字段將其值重置為默認值時,我想簡單一些。 這是我的代碼:
<input id="return" type="checkbox" value="" name="return">Return Journey
<div class="bkt_return_journey" id="altway" style="display: none;">
<input type="text" name="datebr" id="datepickerr" class="booktaxti_input" /></div>
<script type="text/javascript">
$('#return').live('change', function(){
if ( $(this).is(':checked') ) {
$('#altway').show();
} else {
$('#altway').hide();
}
});
</script>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<input id="return" type="checkbox" value="" name="return">Return Journey
<div class="bkt_return_journey" id="altway" style="display: none;">
<input type="text" name="datebr" id="datepickerr" class="booktaxti_input" />
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#return').on('click', function(){
if ( $(this).is(':checked') ) {
$('#datepickerr').val('');
$('#altway').show();
} else {
$('#altway').hide();
}
});
});
</script>
</body>
希望它可以幫助你
<input id="return" type="checkbox" value="" name="return">Return Journey
<div class="bkt_return_journey" id="altway" style="display: none;">
<input type="text" name="datebr" id="datepickerr" class="booktaxti_input" /></div>
<script type="text/javascript">
$(document).on('click','#return', function(){
if ( $(this).is(':checked') ) {
$('#altway').show();
} else {
$('#altway').hide();
}
});
</script>
您可以按以下方式更改代碼:
$('#return').on('change', function () {
if ($(this).is(':checked')) {
$('#datepickerr').val('');
$('#altway').show();
} else {
$('#altway').hide();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.