如果单击无线电,则输入并获取值

时间:2017-11-24 08:35:12

标签: javascript jquery html

我有3个无线电输入和1个文本输入。 "禁用"禁用文本输入属性。我想要做的是当我点击第三个无线电输入时,我的文本输入应该是活动的。当我点击另一个收音机输入时,我的文本输入应该再次被禁用。接下来我想将值传递给span元素。因此,当我点击第一个收音机时,我的跨度应该具有此值等。我不知道如何在启用时从文本输入中获取值。



    $('#sause-oth').on('click', function(){
            $("#sause-other").prop("disabled", false);
    });
    $('.radio').on('click', function(){
        $("#sause-other").prop("disabled", true);
    });

    $('input[name="sause"]').on('change', function(){
        $('#summary_sause').html($(this).val());
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
    <label for="sause-light">Sos ladgodny</label> <br>
    <input type="radio" name="sause" id="sause-light" value="sos lagodny" class="radio"> <br>
    <label for="sause-hot">Sos ostry</label> <br>
    <input type="radio" name="sause" id="sause-hot" value="sos ostry" class="radio"> <br>
    <label for="sause-other">Sos inny</label> <br>
    <input type="radio" name="sause" id="sause-oth" class="enable">
    <input type="text" name="sause-other" id="sause-other" disabled="disabled">
</section>

<hr>

<p>Sos: <span id="summary_sause"></span></p>
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:2)

要从文本中获取值,请将其放在span

$( "#sause-other" ).keyup(function() {
      var text = $(this).val();
      $( "#summary_sause" ).html( text );
});

您可以总结现有代码:

 $('input[name="sause"]').on('change', function() {
     var value = $(this).val();

     if (value == "on") {
       $("#sause-other").prop("disabled", false);
       $("#summary_sause").html("");
     } else {
       $("#sause-other").prop("disabled", true);
       $("#summary_sause").html(value);
     }
   });

链接: https://jsfiddle.net/832wnhpt/1/

答案 1 :(得分:1)

这应该有效:

&#13;
&#13;
$(function(){
  $('[name=sause]').on('change', function() {
    if( $(this).attr('id') == 'sause-oth' ) {
      $("#sause-other").prop("disabled", false);
    }
    else {
      $("#sause-other").prop("disabled", true).val('');
      $('#summary_sause').html($(this).val());
    }
  });
  /* changes the text when focus is out of the field */
  $('#sause-other').on('change', function() {
    $('#summary_sause').html($(this).val());
  });
  /* changes the text on key release */
  /*
  $('#sause-other').on('keyup', function() {
    $('#summary_sause').html($(this).val());
  });
  */
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
  <label for="sause-light">Sos ladgodny</label> <br>
  <input type="radio" name="sause" id="sause-light" value="sos lagodny" class="radio"> <br>
  <label for="sause-hot">Sos ostry</label> <br>
  <input type="radio" name="sause" id="sause-hot" value="sos ostry" class="radio"> <br>
  <label for="sause-other">Sos inny</label> <br>
  <input type="radio" name="sause" id="sause-oth" class="enable">
  <input type="text" name="sause-other" id="sause-other" disabled="disabled">
</section>

<hr>

<p>Sos: <span id="summary_sause"></span></p>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

像这样更新您的代码以进行更改

$('#sause-oth').on('click', function(){
            $("#sause-other").prop("disabled", false);
    });
    $('.radio').on('click', function(){
        $("#sause-other").prop("disabled", true);
    });

    $('input[name="sause"]').on('change', function(){
        add($(this).val());
    });
    
    function add(value){
      $('#summary_sause').html(value);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
    <label for="sause-light">Sos ladgodny</label> <br>
    <input type="radio" name="sause" id="sause-light" value="sos lagodny" class="radio"> <br>
    <label for="sause-hot">Sos ostry</label> <br>
    <input type="radio" name="sause" id="sause-hot" value="sos ostry" class="radio"> <br>
    <label for="sause-other">Sos inny</label> <br>
    <input type="radio" name="sause" id="sause-oth" class="enable" value="">
    <input onkeyup="add(this.value)" type="text" name="sause-other" id="sause-other" disabled="disabled">
</section>

<hr>

<p>Sos: <span id="summary_sause"></span></p>

答案 3 :(得分:0)

我不确定我是否正确理解了你想要的东西,但我想你需要这样的东西:

$('#sause-oth').on('click', function() {
  $("#sause-other").prop("disabled", false);
});

$('.radio').on('click', function() {
  $("#sause-other").prop("disabled", true);
});

$('#sause-other').on('keyup', function() {
  $('#summary_sause').html($(this).val());
});

$('input[name="sause"]').on('change', function() {
  var val = this.id === 'sause-oth' ? $('#sause-other').val() : $(this).val();
  $('#summary_sause').html(val);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
  <label for="sause-light">Sos ladgodny</label> <br>
  <input type="radio" name="sause" id="sause-light" value="sos lagodny" class="radio"> <br>
  <label for="sause-hot">Sos ostry</label> <br>
  <input type="radio" name="sause" id="sause-hot" value="sos ostry" class="radio"> <br>
  <label for="sause-other">Sos inny</label> <br>
  <input type="radio" name="sause" id="sause-oth" class="enable">
  <input type="text" name="sause-other" id="sause-other" disabled="disabled">
</section>

<hr>

<p>Sos: <span id="summary_sause"></span></p>

答案 4 :(得分:0)

据我所见,其他答案在您选择其他按钮然后返回文本时未涵盖此案例。试试这个。

    $('#sause-oth').on('click', function(){
            $("#sause-other").prop("disabled", false);
    });
    $('.radio').on('click', function(){
        $("#sause-other").prop("disabled", true);
    });

    $('input[name="sause"]').on('change', function(){
        $('#summary_sause').html($(this).val());
    });

    $( "#sause-other" ).on('input', function() {
        $('#summary_sause').html($(this).val());
        $("#sause-oth").val($(this).val());
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
    <label for="sause-light">Sos ladgodny</label> <br>
    <input type="radio" name="sause" id="sause-light" value="sos lagodny" class="radio"> <br>
    <label for="sause-hot">Sos ostry</label> <br>
    <input type="radio" name="sause" id="sause-hot" value="sos ostry" class="radio"> <br>
    <label for="sause-other">Sos inny</label> <br>
    <input type="radio" name="sause" id="sause-oth" class="enable">
    <input type="text" name="sause-other" id="sause-other" disabled="disabled">
</section>

<hr>

<p>Sos: <span id="summary_sause"></span></p>