jquery常用语句

dropdownlist:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

    <script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $("#ddlGuo").change(function() {
            alert($("#ddlGuo option:selected").val());
            alert($("#ddlGuo option:selected").text());
                var a = $("#ddlGuo option:selected").val();
                var b = $("#ddlGuo option:selected").text();
                $("#txttext").attr("value", b);
                $("#txtval").attr("value", a);
            });

        });
 
    
    </script>
</head>
<body  >
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlGuo" runat="server"  >
            <asp:ListItem Selected="True" Value="1">C国</asp:ListItem>
            <asp:ListItem Value="2">D国</asp:ListItem>
            <asp:ListItem Value="3">E国</asp:ListItem>
      
        </asp:DropDownList>
        <asp:TextBox ID="txtval" runat="server"></asp:TextBox>
        <asp:TextBox ID="txttext" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

 

radio

获取一组radio被选中项的值
var item = $('input[@name=items][@checked]').val();
获取select被选中项的文本
var item = $("select[@name=items] option[@selected]").text();
select下拉框的第二个元素为当前选中值
$('#select_id')[0].selectedIndex = 1;
radio单选组的第二个元素为当前选中值
$('input[@name=items]').get(1).checked = true;

获取值:

文本框,文本区域:$("#txt").attr("value");
多选框checkbox:$("#checkbox_id").attr("value");
单选组radio:   $("input[@type=radio][@checked]").val();
下拉框select: $('#sel').val();

控制表单元素:
文本框,文本区域:$("#txt").attr("value",'');//清空内容
                 $("#txt").attr("value",'11');//填充内容

多选框checkbox: $("#chk1").attr("checked",'');//不打勾
                 $("#chk2").attr("checked",true);//打勾
                 if($("#chk1").attr('checked')==undefined) //判断是否已经打勾

单选组radio:    $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
下拉框select:   $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
                $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option
                $("#sel").empty();//清空下拉框

 

radiobuttonlist

var a = $("#<%=rblisflag.ClientID %>").find("input[@checked]").val();

var a=$(':radio[name=rbl]:checked').val();

获取name标签

 $("input[name=cktongyi]")

 

ajax 返回值:

 

 

  1. function checkCode(){   
  2.     var temp;   
  3.     $.ajax({        
  4.        cache:false,   
  5.         async:false,   
  6.         type:"POST",       
  7.         url:"doJSP/doCode.jsp",      
  8.         data:"rand="+code.val(),     
  9.         success:function(rand){   
  10.             if(parseInt(code.val())==parseInt(rand) && code.val()!=""){   
  11.                 right(code,codeError);   
  12.                 codeError.html(" 验证码输入正确.");   
  13.                 temp=true;   
  14.             }else{   
  15.                 temp2="false";   
  16.                 wrong(code,codeError);   
  17.                 codeError.html(" 验证码输入错误,可以点击图片刷新.");   
  18.                 temp=false;   
  19.             }   
  20.         }   
  21.    });   
  22.    return temp;  

dropdownlist:

var s_flag=true;
            for(var i=0;i<$(".ddl_class option:selected").length;i++)
            {
                if($($(".ddl_class option:selected")[i]).val()=="1")
                {
                    s_flag=false;
                    break;
                }
            }
           
            if(s_flag==false)
            {
                alert("请选择眼睛度数!");
                return false;
            }

<asp:DropDownList ID="ddlright" class="ddl_class" runat="server">
 </asp:DropDownList>

数组:

$(function(){
            //加载初始化
            var count=$(".pro_class").length;
            var arr_class=new Array();//数组定义
            if(count>0)
            {
                //pro_class
                for(var i=0;i<count;i++)
                {
                    var r=$(".pro_class")[i];
                    var s=$(r).attr("class").split(" ")[1];
                    var index = $.inArray(s,arr_class);//判断数组是否存在某元素 存在返回坐标值 不存在返回-1
                    if(index==-1)
                    {
                        arr_class.push(s);//往数组中添加元素
                    }
                }
                if(arr_class.length>0)
                {
                    for(var j=0;j<arr_class.length;j++)
                    {
                        //pro_class_139
                        var pid=arr_class[j].split("_")[2];                        
                        var child_count=$("."+arr_class[j]).length;
                        if(child_count>0)
                        {
                            var r_child=$("."+arr_class[j])[0];
                            if(r_child.type=="radio")
                            {
                                r_child.checked=true;
                                $("."+pid).val(r_child.value);
                            }
                        }
                    }
                }
            }
            
            //颜色点击
            $(".pro_class").click(function(){
                var s=$(this).attr("class").split(" ")[1];
                var pid=s.split("_")[2];
                var child_count=$("."+s).length;
                if(child_count>0)
                {
                    for(var j=0;j<child_count;j++)
                    {
                        var r_child=$("."+s)[j];
                        if(r_child.type=="radio")
                        {
                            if(r_child.checked==true)
                            {
                                $("."+pid).val(r_child.value);
                            }
                        }
                    }
                }
                $("#big_"+pid).attr("src","../product/photo/"+$(this).attr("id"));            
            });
        }); 

posted on 2010-05-08 11:47  mingfeng  阅读(1516)  评论(0编辑  收藏  举报

导航