获取变量的值radio_button jquery

时间:2016-05-17 08:10:26

标签: jquery radio-button

我需要获取所选radio_button的值。首先,向用户显示,然后使用此变量。该值存储在数据库中,因此脚本中存在问题。

$(document).ready(function() {

  $('input[name="timeselect"]:radio').change(function() {

    var str3 = "";
    $('input[name="timeselect"]:checked').val();
    str3 += $(this).text() + "\n";

    $("#timevalue").text();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form class="new_order" id="new_order" action="/orders" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="&#x2713;" />
  <input type="hidden" name="authenticity_token" value="GCE2" />
  <div class="table-responsive">
    <table class="table table-striped">
      <tbody>
        <input class="timeselect" type="radio" value="1" name="order[route_id]" id="order_route_id_1" />
        <label for="order_route_id_1">07:00</label>
        <input class="timeselect" type="radio" value="2" name="order[route_id]" id="order_route_id_2" />
        <label for="order_route_id_2">07:00</label>
        <input class="timeselect" type="radio" value="3" name="order[route_id]" id="order_route_id_3" />
        <label for="order_route_id_3">09:00</label>
        <input class="timeselect" type="radio" value="4" name="order[route_id]" id="order_route_id_4" />
        <label for="order_route_id_4">09:00</label>
      </tbody>
    </table>
  </div>
  <button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Далее</button>
  <p> <span id="timevalue"></span>
  </p>
  <input type="submit" name="commit" value="Заказать" class="btn btn-success btn-lg pull-right" />
</form>

1 个答案:

答案 0 :(得分:0)

您的选择器不正确。 timeselect定义为CSS类,因此使用Class Selector (“.class”)

var str3 = $('input.timeselect:checked').val();
带有Attribute Equals Selector [name=”value”]

name="order[route_id]"

var str3 = $('input[name="order[route_id]"]:checked').val();

您需要将其分配给变量str3,截至目前您已获取checked单选按钮值但根本不使用它。

$(document).ready(function() {

  $('input.timeselect:radio').change(function() {
    var str3 = $('input.timeselect:checked').val();
    str3 += $(this).text() + "\n";

    $("#timevalue").text(str3);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form class="new_order" id="new_order" action="/orders" accept-charset="UTF-8" method="post">
  <input name="utf8" type="hidden" value="&#x2713;" />
  <input type="hidden" name="authenticity_token" value="GCE2" />
  <div class="table-responsive">
    <table class="table table-striped">
      <tbody>
        <input class="timeselect" type="radio" value="1" name="order[route_id]" id="order_route_id_1" />
        <label for="order_route_id_1">07:00</label>
        <input class="timeselect" type="radio" value="2" name="order[route_id]" id="order_route_id_2" />
        <label for="order_route_id_2">07:00</label>
        <input class="timeselect" type="radio" value="3" name="order[route_id]" id="order_route_id_3" />
        <label for="order_route_id_3">09:00</label>
        <input class="timeselect" type="radio" value="4" name="order[route_id]" id="order_route_id_4" />
        <label for="order_route_id_4">09:00</label>
      </tbody>
    </table>
  </div>
  <button class="btn btn-primary nextBtn btn-lg pull-right" type="button">Далее</button>
  <p> <span id="timevalue"></span>
  </p>
  <input type="submit" name="commit" value="Заказать" class="btn btn-success btn-lg pull-right" />
</form>