zoukankan      html  css  js  c++  java
  • jquery单选框radio绑定click事件实现和是否选中的方法

    使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项:

    1.<input type="radio" name="testradio" value="jquery获取radio的值" />jquery获取radio的值
    2.<input type="radio" name="testradio" value="jquery获取checkbox的值" />jquery获取checkbox的值
    3.<input type="radio" name="testradio" value="jquery获取select的值" />jquery获取select的值
    要想获取某个radio的值有以下的几种方法,直接给出代码:

    1、$('input[name="testradio"]:checked').val();
    2$('input:radio:checked').val();
    3$('input[@name="testradio"][checked]');
    4$('input[name="testradio"]').filter(':checked');

    差不多挺全的了,如果我们要遍历name为testradio的所有radio呢,代码如下
    $('input[name="testradio"]').each(function(){alert(this.value);});

    如果要取具体某个radio的值,比如第二个radio的值,这样写
    $('input[name="testradio"]:eq(1)').val()

    下面实例讲述了jquery单选框radio绑定click事件实现方法。分享给大家供大家参考。

    具体实现方法如下:

    <html>
    <head>
    <title>单选框radio绑定click事件</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
     $(function(){
      $(":radio").click(function(){
       alert("您是..." + $(this).val());
      });
     });
    </script>
    <style type="text/css">
    h5{color:blue;}
    textarea{resize:none;}
    #word{color:red;}
    </style>
    </head>
    <body>
    <h5>单选框 radio 绑定 click 事件</h5>
    <form action="" method="">
     <input type="radio" name="gender" value="男性">男性
     <input type="radio" name="gender" value="女性">女性
     <input type="radio" name="gender" value="春哥">春哥
    </form>
    </body>
    </html>
  • 相关阅读:
    网络流练习
    Noip2018 游记
    4719: [Noip2016]天天爱跑步
    1875: [SDOI2009]HH去散步
    P2619 [国家集训队2]Tree I
    1493: [NOI2007]项链工厂
    P1710 地铁涨价
    P3694 邦邦的大合唱站队
    P1439 【模板】最长公共子序列
    P1132 数字生成游戏
  • 原文地址:https://www.cnblogs.com/shenzikun1314/p/6843364.html
Copyright © 2011-2022 走看看