admin管理员组

文章数量:1289384

I have a bootstrap radio button which needs to get selected based on what value the user gives. Suppose the user enters 'male', male radio button should be selected. Consider the user enters the value in lower case.

    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=".7.0/css/font-awesome.min.css">
<link rel="stylesheet" href=".3.7/css/bootstrap.min.css">
<script src=".11.2/jquery.min.js"></script>
<script src=".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()"/>
    
    

I have a bootstrap radio button which needs to get selected based on what value the user gives. Suppose the user enters 'male', male radio button should be selected. Consider the user enters the value in lower case.

    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./ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./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()"/>
    
    

Share Improve this question edited Jul 7, 2017 at 6:43 coder7777 asked Jul 6, 2017 at 12:05 coder7777coder7777 3781 gold badge5 silver badges16 bronze badges 1
  • if condition for male $('input[value=M]').parent('label').addClass('active').siblings("label").removeClass('active'); for female $('input[value=F]').parent('label').addClass('active').siblings("label").removeClass('active'); – Kanu Commented Jul 6, 2017 at 12:14
Add a ment  | 

7 Answers 7

Reset to default 4

    function check(){
    var val = document.getElementById("inp").value;
    if( val == "male"){
        $("input[value='M']").click();
        }
    if(val == "female"){
         $("input[value='F']").click();
        }
    }
    
    
<link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./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: 10px;" value="M" type="radio" name="gender" />M</label>
                  <label class="btn btn-default">
                    <input class="form-control input-sm" type="radio" value="F" name="gender" />F</label>
                  
                </div><br><br>
    <input id="inp" onKeyup="check()" type="text"/>
    
    

You can just use JS to do document.getElementById("rdGenderElement").checked = {true|false};

Example

document.getElementById("btnChangeRdGenderM").addEventListener('click', function(){
    document.getElementById("rdGenderM").checked = true;
});
document.getElementById("btnChangeRdGenderF").addEventListener('click', function(){
    document.getElementById("rdGenderF").checked = true;
});
.gender{
    width: 100%;
}

button{
    padding: 10px 30px;
    margin: 5px;
}
<button id="btnChangeRdGenderM">Select M</button>
<button id="btnChangeRdGenderF">Select F</button>

<hr/>

<div class="gender">
M <input id="rdGenderM" type="radio" name="gender" />

<br/>
F <input id="rdGenderF" type="radio" name="gender" />
</div>

You can use jquery builtin function Click() for this task.

    function check(){
    var val = document.getElementById("inp").value;
    if( val == "male"){
      $('#male').click();
      console.log(val);
    }
    if(val == "female"){
      $('#female').click();
     console.log(val);
     }
    }
    
<link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./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: 10px;" value="M" type="radio" name="gender" id="male" />M</label>
                  <label class="btn btn-default">
                    <input class="form-control input-sm" type="radio" value="F" name="gender" id="female" />F</label>
                  
                </div><br><br>
    <input id="inp" type="text" onblur="check()"/>
    
    

Try with id attribute and use toLowerCase() convert the value to lowercase

function check(){
    var val = document.getElementById("inp").value;
    console.log(val)
   document.getElementById(val.trim().toLowerCase()).click();
        }
<link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./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: 10px;" value="M" type="radio" name="gender" id="male" checked/>M</label>
                  <label class="btn btn-default">
                    <input class="form-control input-sm" id="female"type="radio" value="F" name="gender" />F</label>
                  
                </div><br><br>
    <input id="inp" type="text" onblur="check()"/>

Try below snippets,

function check(){
    var val = document.getElementById("inp").value;

    var label_male_ = document.getElementById('male');
    var chk_male_ = document.getElementById('chk_male');

    var label_female_ = document.getElementById('female');
    var chk_female_ = document.getElementById('chk_female');
    // Set default.
    label_male_.className += 'btn btn-default';
    chk_male_.checked = false;
    // Set default.
    label_female_.className += 'btn btn-default';
    chk_female_.checked = false;
    if( val == "male"){
        //check male radio button
        label_male_.className += ' active';
        chk_male_.checked = true;
    }
    if(val == "female"){
        //check female radio button
        label_female_.className += ' active';
        chk_male_.checked = true;
    }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./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" id="male">
        <input class="form-control input-sm" style="width: 10px;" value="M" type="radio" name="gender" id="chk_male" />M
    </label>
    <label class="btn btn-default" id="female">
        <input class="form-control input-sm" type="radio" value="F" name="gender" id="chk_female" />F
    </label>
</div><br /><br />
<input id="inp" type="text" onblur="check()"/>

You've included jquery, so I'm using that to replace your "check" function and bind it to the input. Instead of assuming the text is lowercase, we'll coerce it manually with toLowerCase(). Note the selectors for the inputs of type radio with a matching value. Also note that we set the property 'checked' instead of older methods.

// bind keyup events on the input textbox
$(document).on('keyup', '#inp', function(e) {
    // get a lowercase version of the input text
    var input = $(this).val().toLowerCase();
    // do a parison and set the appropriate radio
    if (input == 'male')
        $('input:radio[value=M]').prop('checked', true);
    else if (input == 'female')
        $('input:radio[value=F]').prop('checked', true);
});

I prefer to use .on() for adding event handlers because it matches the .off() method for removing them. Binding to $(document) like this guarantees that the event handlers will run even if the form elements didn't exist when the page was creates - great for dynamically generated forms. Combining these gives you excellent control for binding and unbinding event handlers based on need and focus.

First notice that you have to match user input value to some value of element so that It could be selected dynamically otherwise you have to define it already that which element would be selected throughout the array of elements. So I have changed your input values a little bit to "male" and "female". Then it would be easy to perform checking operation based on user input and radio button values.

function check()
{
  var val = document.getElementById("inp").value;
  var elements = document.getElementsByName("gender");
  for(i of elements)
  {
      if(val == i.value)
      {
        i.checked= true;
        i.parentNode.classList.add("active");
     } 
     else
     {
      if(i.checked)
      i.parentNode.classList.remove("active");
     }
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./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: 10px;" value="male" type="radio" name="gender" />M</label>
                  <label class="btn btn-default">
                    <input class="form-control input-sm" type="radio" value="female" name="gender" />F</label>
                  
                </div><br><br>
    <input id="inp" type="text" onblur="check()"/>

本文标签: javascriptHow to check a radio button based on some other valueStack Overflow