[英]How Can I Manually Toggle Radio Button With a Dropdown List
我使用Bootstrap创建了两个单选按钮,第一个仅带有标签,第二个具有包含名称的下拉列表。 如果用户打开列表并选择一个名称,我想使用jquery来更新单选按钮,但我不知道如何实现它。 这是显示我所拥有的jsfiddle 。
奖励积分,如果您可以向我展示如果取消选中他们可能选择的所有学生的话,如何重新选择第一个选项!
<div class="col-xs-4">
<label>Students to Export:</label>
</div>
<div class="col-xs-4">
<label class="radio-inline">
<input checked="checked" name="expStudents" type="radio" previousvalue="checked"> This Student Only
</label>
</div>
<div class="col-xs-4">
<label class="radio-inline">
<input name="expStudents" type="radio" previousvalue="false">
<button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<span class="glyphicon glyphicon-user"></span> Multiple Students <span class="caret"></span>
</button>
<ul id="ddStudents" class="dropdown-menu" style="width:300px; max-height: 400px; overflow-y: scroll" onclick="radioToggle();">
<li>
<input type="checkbox" class="ddStudents" id="inpStudent1055469" value="1055469 " onclick="ddInputsChanged('#ddStudents input', '#inpStudent0');">
<label for="inpStudent1055469" class="input small dropdownLabel_NormalFont">Student 1</label>
</li>
<li>
<input type="checkbox" class="ddStudents" id="inpStudent906219" value="906219 " onclick="ddInputsChanged('#ddStudents input', '#inpStudent0');">
<label for="inpStudent906219" class="input small dropdownLabel_NormalFont">Student 2</label>
</li>
<li>
<input type="checkbox" class="ddStudents" id="inpStudent852554" value="852554 " onclick="ddInputsChanged('#ddStudents input', '#inpStudent0');">
<label for="inpStudent852554" class="input small dropdownLabel_NormalFont">Student 3</label>
</li>
</ul>
</label>
</div>
function radioToggle(val) {
$('input[name=expStudents]').click(function () {
if ($(this).attr('previousValue') == 'true') {
$(this).attr('checked', false)
} else {
$('input[name=expStudents]').attr('previousValue', false);
}
$(this).attr('previousValue', $(this).attr('checked'));
});
}
我在您的两个单选按钮中添加了ID。
$('input[type="checkbox"]').change(function() { var check = 0; $('input[type="checkbox"]').each(function () { if(this.checked) { check++; } }); if (check == 0) { $('#onlyStudent').prop('checked', true); }else if (check > 0) { $('#moreStudents').prop('checked', true); } });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="col-xs-4"> <label>Students to Export:</label> </div> <div class="col-xs-4"> <label class="radio-inline"> <input checked="checked" id="onlyStudent" name="expStudents" type="radio" previousvalue="checked"> This Student Only </label> </div> <div class="col-xs-4"> <label class="radio-inline"> <input name="expStudents" id="moreStudents" type="radio" previousvalue="false"> <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> <span class="glyphicon glyphicon-user"></span> Multiple Students <span class="caret"></span> </button> <ul id="ddStudents" class="dropdown-menu" style="width:300px; max-height: 400px; overflow-y: scroll"> <li> <input type="checkbox" class="ddStudents" id="inpStudent1055469" value="1055469 "> <label for="inpStudent1055469" class="input small dropdownLabel_NormalFont">Student 1</label> </li> <li> <input type="checkbox" class="ddStudents" id="inpStudent906219" value="906219 "> <label for="inpStudent906219" class="input small dropdownLabel_NormalFont">Student 2</label> </li> <li> <input type="checkbox" class="ddStudents" id="inpStudent852554" value="852554 "> <label for="inpStudent852554" class="input small dropdownLabel_NormalFont">Student 3</label> </li> </ul> </label> </div>
如果您只需要根据选中的复选框来切换单选按钮,则可以:
$('#ddStudents :checkbox').on('change', function (e) { var currRadio = $(this).closest('.radio-inline').find('[name="expStudents"]'); if ($('#ddStudents :checkbox:checked').length > 0) { currRadio.prop('checked', true); } else { $('[name="expStudents"]').not(currRadio).prop('checked', true); } }); function ddInputsChanged () { ; // only for demo }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="col-xs-4"> <label>Students to Export:</label> </div> <div class="col-xs-4"> <label class="radio-inline"> <input checked="checked" name="expStudents" type="radio" previousvalue="true"> This Student Only </label> </div> <div class="col-xs-4"> <label class="radio-inline"> <input name="expStudents" type="radio" previousvalue="false"> <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> <span class="glyphicon glyphicon-user"></span> Multiple Students <span class="caret"></span> </button> <ul id="ddStudents" class="dropdown-menu" style="width:300px; max-height: 400px; overflow-y: scroll"> <li> <input type="checkbox" class="ddStudents" id="inpStudent1055469" value="1055469 " onclick="ddInputsChanged('#ddStudents input', '#inpStudent0');"> <label for="inpStudent1055469" class="input small dropdownLabel_NormalFont">Student 1</label> </li> <li> <input type="checkbox" class="ddStudents" id="inpStudent906219" value="906219 " onclick="ddInputsChanged('#ddStudents input', '#inpStudent0');"> <label for="inpStudent906219" class="input small dropdownLabel_NormalFont">Student 2</label> </li> <li> <input type="checkbox" class="ddStudents" id="inpStudent852554" value="852554 " onclick="ddInputsChanged('#ddStudents input', '#inpStudent0');"> <label for="inpStudent852554" class="input small dropdownLabel_NormalFont">Student 3</label> </li> </ul> </label> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.