2017-08-07 68 views
1

我希望能够取消选中复选框,如果我已经使用jQuery选择了我的下拉菜单。我觉得我可能几乎在那里,但不知道为什么它不工作。例如,我点击MR,然后我改为医生 - MR选择。设置一个复选框,当点击下拉框时取消选中

<script> 
$('.multiple-choice').click(function(){ 
$('input').prop('checked', false); 
}); 
</script> 

<fieldset class="title-box"> 

<span><input type="radio" name="title" value="Mr" onclick="titlehandler(this)" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span> 
<span><input type="radio" name="title" value="Mrs" onclick="titlehandler(this)" id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span> 
<span><input type="radio" name="title" value="Miss" onclick="titlehandler(this)" id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span> 
<span><input type="radio" name="title" value="Ms" onclick="titlehandler(this)" id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span> 

    <span><label for="dropdown">Other</label> 
    <select name="dropdown" id="dropdown" class="multiple-choice"> 


<option value="Dr" name="othertitle1" id="othertitle1">Dr</option> 
<option value="Rev">Rev</option> 
<option value="Sir">Sir</option> 
<option value="Sist">Sist</option></select> 
</fieldset> 

<script> 
 
\t $('.multiple-choice').click(function(){ 
 
    $('input').prop('checked', false); 
 
    }); 
 
    </script>
<fieldset class="title-box"> 
 
\t \t \t \t \t \t \t 
 
\t <span><input type="radio" name="title" value="Mr" onclick="titlehandler(this)" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span> 
 
\t <span><input type="radio" name="title" value="Mrs" onclick="titlehandler(this)" id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span> 
 
\t <span><input type="radio" name="title" value="Miss" onclick="titlehandler(this)" id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span> 
 
\t <span><input type="radio" name="title" value="Ms" onclick="titlehandler(this)" id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span> 
 

 
\t \t <span><label for="dropdown">Other</label> 
 
\t \t <select name="dropdown" id="dropdown" class="multiple-choice"> 
 
          
 

 
    <option value="Dr" name="othertitle1" id="othertitle1">Dr</option> 
 
    <option value="Rev">Rev</option> 
 
    <option value="Sir">Sir</option> 
 
    <option value="Sist">Sist</option></select> 
 
    </fieldset>

+2

将您的JS代码_after_ HTML标记。目前它在HTML行加载之前执行。 –

+0

或者将它包装在'$(document).ready'中。 – briosheje

回答

1

试试这个代码:

<script> 
$('.multiple-choice').click(function(){ 
    $("input[type='radio'").each(function() { 
     $(this).prop('checked', false); 
    }); 
}); 
</script> 
+0

谢谢,如果我想单击回标题时想清除下拉菜单,我也会使用此代码,但是会反转? – Xoog

1

无论是在你的文件底部把这部分或一个$(document).ready()回调包裹。

<script> 
$('.multiple-choice').click(function(){ 
$('input').prop('checked', false); 
}); 
</script> 
+0

谢谢!如果我想单击回标题时想清除下拉列表,我也会使用此代码,但是会反转? – Xoog

+0

是的。这应该工作。 – Difster

1

我删除onclick="titlehandler(this)",它似乎是做工精细:

$('.multiple-choice').click(function() { 
 
    $('input').prop('checked', false); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<fieldset class="title-box"> 
 

 
    <span><input type="radio" name="title" value="Mr" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span> 
 
    <span><input type="radio" name="title" value="Mrs" id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span> 
 
    <span><input type="radio" name="title" value="Miss" id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span> 
 
    <span><input type="radio" name="title" value="Ms" id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span> 
 

 
    <span><label for="dropdown">Other</label> 
 
    <select name="dropdown" id="dropdown" class="multiple-choice"> 
 

 

 
<option value="Dr" name="othertitle1" id="othertitle1">Dr</option> 
 
<option value="Rev">Rev</option> 
 
<option value="Sir">Sir</option> 
 
<option value="Sist">Sist</option></select> 
 
</fieldset>

相关问题