jQuery操作radio、checkbox和select示例

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>jQuery操作radio、checkbox和select示例</title>
</head>
<body>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //radio
            $(":radio[name='myradio']").eq(0).attr("checked", true);
            alert($(":radio[name='myradio']:checked").val());
            //checkbox
            $(":checkbox[name='mycheckbox']").eq(1).attr("checked", true);
            alert($(":checkbox[name='mycheckbox']:checked").val());
            //select
            $("select[name='myselect']").append("<option value=\"sv3\">st3</option>");
            $("select[name='myselect']").val("sv3");
            $("select[name='myselect']").val("sv3").change();
            alert($("select[name='myselect'] option:selected").val());            
            alert($("select[name='myselect'] option:selected").text());
        });
    </script>

    <!-- radio -->
    <input name="myradio" type="radio" value="rv1">rt1
    <input name="myradio" type="radio" value="rv2">rt2
    <input name="myradio" type="radio" value="rv3">rt3
    <!-- checkbox -->
    <input name="mycheckbox" type="checkbox" value="cv1">ct1
    <input name="mycheckbox" type="checkbox" value="cv2">ct2
    <input name="mycheckbox" type="checkbox" value="cv3">ct3
    <!-- select -->
    <select name="myselect">
        <option value="sv1">st1</option>
        <option value="sv2">st2</option>
    </select>
</body>
</html>


评论: 0 | 引用: 0 | 查看次数: 3301
发表评论
登录后再发表评论!