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>
  • 相关阅读:
    php安装xcache (5.4)
    nginx博客系统(内含nginx图片缩略图处理代码,不错)
    一个mysql开启多个端口
    mysql源码重启
    ecshop支付时减库存方法
    n阶幻方问题
    codeforces 710A King Moves(水)
    关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
    codeforces 701C. They Are Everywhere(尺取法)
    codeforces 701 B. Cells Not Under Attack
  • 原文地址:https://www.cnblogs.com/shenzikun1314/p/6843364.html
Copyright © 2011-2022 走看看