如何从选定的单选按钮获取值

时间:2016-06-26 16:31:18

标签: javascript jquery

先生,我有输入文字和单选按钮等表格输入。 如何获取所选单选按钮的值,并将该值放入输入类型文本。

heres jsfiddle,https://jsfiddle.net/czw71dfk/2/

    <div id="gender" class="form-inline">
  <div class="form-group">
    <label for="">I Sign up as</label>
    <input #id="place" type="text" class="form-control" readonly/>
    <div class="radio-inline">
      <label class="label_item" for="radioMale">
        <input type="radio" class="radio_item" value="male"  name="gender" id="radioMale">
      </label>
      <p class="text-center colorGrey">Male</p>
     </div>
    <div class="radio-inline">
      <label class="label_item" for="radioFemale">
        <input type="radio" class="radio_item" value="female"  name="gender" id="radioFemale">
      </label>
      <p class="text-center colorGrey">Female</p>
    </div>
    <div class="radio-inline">
      <label class="label_item" for="radioKids">
        <input type="radio" class="radio_item" value="Kids" name="gender" id="radioKids">
      </label>
      <p class="text-center colorGrey">Kids</p>
    </div>
  </div>
</div>

$('#gender input').on('change', function() {
    var gender = $( this ).val();
   $("#place").val( gender );
});

2 个答案:

答案 0 :(得分:1)

#id="place"更改为id="place" https://jsfiddle.net/czw71dfk/4/

<input #id="place" type="text" class="form-control" readonly/>

<input id="place" type="text" class="form-control" readonly/>

答案 1 :(得分:1)

HTML问题#id="place"

必须在改变时使用;

$(document).ready(function(){

  $('input[name=gender]').on('change', function() {
    var gender = $( this ).val();
     $("#place").val( gender );
  });

});

JSFiddle.net Example