如何从javascript中的id复选框中获取值

时间:2014-04-18 13:02:23

标签: javascript jquery

的jQuery

$(document).ready(function(){
  $("#cityshow").click(function(){
    $("#citybox").toggle();
  });

    $('#citybox input[type=radio]').click(function(){
        var buttonValue = $(this).val();
        $("#citybox").toggle();
        $('#cityshow').html(buttonValue);
    });
});

HTML

<form action="search.php" name="search" method="get" id="search">
       <input type="hidden" value="" id="cityshow">
        <div id="cityshow" class="citybox">Sikar</div>
       <div id="citybox" class="citycollection">
       <div style="height:0px; border-bottom:1px solid #333; width:62.5%; margin:0px; float:right   "></div>
       <div  style="width:100%; padding:20px 15px;">
       <ul>
           <li><input id="Neem" type="radio" name="sikar" value="Neem Ka Thana"> <label for="Neem">Neema Ka Thana</label></li>
           <li><input id="shrimado" type="radio" name="sikar" value="Shri Madhopur"> <label for="shrimado">Shri Madhopur</label></li>
           <li><input id="danta" type="radio" name="sikar" value="Danta Ramgarh"> <label for="danta">Danta Ramgarh</label></li>
           <li><input id="sikar" type="radio" name="sikar" value="Sikar"> <label for="sikar">Sikar</label></li>
           <li><input id="laxmangarh" type="radio" name="sikar" value="Laxmangarh"> <label for="laxmangarh">Laxmangarh</label></li>
           <li><input id="fatehpur" type="radio" name="sikar" value="Fatehpur"> <label for="fatehpur">Fatehpur</label></li>
       </ul>
       </div>
       </div>
      <input type="text" name="query" id="zipsearch" placeholder="Search By Keywords"  required/>
</form>

如何从JavaScript / jQuery中的id获取a的值?

2 个答案:

答案 0 :(得分:1)

使用.val()

$('#yourid').val();

答案 1 :(得分:0)

此外,这非常好:

$('#yourid').is(':checked');

对于语义,这个更广泛使用。