Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   Получения значения радиокнопки (https://javascript.ru/forum/dom-window/44611-polucheniya-znacheniya-radioknopki.html)

hardware 26.01.2014 13:51

Получения значения радиокнопки
 
Как мне получить допустим в переменную X значения активной радиокнопки по средствам jquery
<div id="discount_cal">
        <input name="discount" type="radio" checked value="1">Без скидки |
        <input name="discount" type="radio" value="5">5% скидка |
        <input name="discount" type="radio" value="10">10% скидка |
        <input name="discount" type="radio" value="20">20% скидка |
        <input name="discount" type="radio" value="30">30% скидка
</div>

:help:

рони 26.01.2014 14:23

hardware,
:-?
<!DOCTYPE HTML>

<html>

<head>
  <title>Untitled</title>
  <meta charset="utf-8">
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>

<body>
<div id="discount_cal">
        <input name="discount" type="radio" checked value="1">Без скидки |
        <input name="discount" type="radio" value="5">5% скидка |
        <input name="discount" type="radio" value="10">10% скидка |
        <input name="discount" type="radio" value="20">20% скидка |
        <input name="discount" type="radio" value="30">30% скидка
</div>
 <script>
    var $cal = $('#discount_cal'),$dis = $('[name=discount]'),
    foo = function ()
{
     alert($(':checked',$cal).val())
};
    foo();
    $cal.on('click', $dis, foo)
 </script>
</body>

</html>

hardware 26.01.2014 14:42

Спасибо

Vlasenko Fedor 26.01.2014 15:09

решение без jQuery
<body>
    <div id="discount_cal">
      <input name="discount" type="radio" checked value="1">Без скидки |
      <input name="discount" type="radio" value="5">5% скидка |
      <input name="discount" type="radio" value="10">10% скидка |
      <input name="discount" type="radio" value="20">20% скидка |
      <input name="discount" type="radio" value="30">30% скидка</div>
    <script>
      discount_cal.onclick = function (e) {
        e = e || window.event;
        target = e.target || e.srcElement;
        if (target.tagName != "INPUT") return;
        alert(target.value);
      }
    </script>
  </body>


Часовой пояс GMT +3, время: 05:09.