我有一個引導單選按鈕,需要根據用戶賦予的值來選擇。假設用戶輸入'男性',應選擇男性單選按鈕。考慮用戶以小寫字母輸入值。如何根據其他值檢查單選按鈕?
function check(){
var val = document.getElementById("inp").value;
if(val == "male"){
//select male radio button
}
if(val == "female"){
//select female radio button
}
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="btn-group" data-toggle="buttons">
<p>Gender <span>*</span></p>
<label class="btn btn-default">
<input class="form-control input-sm" style="width: 0px;" value="M" type="radio" name="gender" />M</label>
<label class="btn btn-default">
<input class="form-control input-sm" style="width: 0px;" type="radio" value="F" name="gender" />F</label>
</div><br><br>
<input id="inp" type="text" onblur="check()"/>
如果條件爲男性 $( '輸入[值= M]')。親本( '標籤')。addClass( '活性')的兄弟姐妹( 「標籤」)removeClass( '激活')。; 女 $('input [value = F]')。parent('label')。addClass('active')。siblings(「label」)。removeClass('active'); – Kanu