Jquery知识总结

一、空格
   $("#id").val(jQuery.trim($("#id").val()));
   $.trim($("#txtDomainName").val()) == ""

二、获取dropdownlist
     获取选中值: $("#ddlList option:selected").text()
  设置选中值: $("#select_id option[value='2']").attr("selected", true); 或者$("#select_id option[text='jQuery']").attr("selected", true);
  var selectObject = $("select[name$='ddlValue']")
  selectObject.empty();//清空option选项
  selectObject.remove();//删除option选项
 
  判断选中
     $(document).ready(function () {
            var selectValue = $.trim($("#hidChartType").val()).toLocaleLowerCase();
            $("#ddlChartType option").each(function () {
                if ($.trim($(this).text()).toLocaleLowerCase() == selectValue) {
                    $(this).attr("selected", true);
                }
            });
        });
 

三、初次加载

  $(document).ready(function () {
            if ($("#rdoAdd").attr("checked")) {

                $("#divCopy").hide();
                $("#divAdd").show();
            }
        });
四、根据id判断 单选按钮是否选中
    $("#rdoAdd").attr("checked")
五、根据name 判断选中的单选按钮
    var id = $("input[name='rdoUser']:checked").attr("title");

六、Jquery 的ajax
    $.ajax({
                      type: "POST", //用post的传输模式
                      dataType: "string", //传输的类型
                      url: 'AddOrdersInfos.ashx', //目标地址
                      data: "p=" + (orders + "⊙" + sqls_orders), //传入变量
                      success: function(json) {
                          if (json == "true") {
                              $("#btn_saveproduct").attr("disabled", true);
                              alert("操作成功!");
                              window.location.href = "searchOrders.aspx";
                          }
                          else {
                              alert("服务器繁忙,请稍后重试!");
                          }
                      },
                      error: function() {
                          alert("数据读取失败!");
                      }
                  });
七、空格
  $.trim()
 $("#id").val(jQuery.trim($("#id").val()));
 $.trim($("#txtDomainName").val()) == ""

八、gridview中radiobutton实现单选
 $('input[type="radio"]').click(function () { 

                 $.each($('input[type="radio"]'), function () { this.checked = false; }); 

                 this.checked = true; 

              }); 

九、获取以""结尾的对象
   $("input[name$='txtDeployTime']")
十、获取以""开头的对象
   $("input[name^='txtDeployTime']")
十一、
       $(document).ready(function () {
            $("#btnAdd").click(function () {
                $("#grvCompont tr").each(function () {
                    var a = $("this td:eq(3)").val();
                    alert(a);

                });
            });
        });

十二、 function checkedGroup(obj) {
            $(obj).parent().parent().parent().find("input[type='checkbox']").attr("checked",$(obj).attr("checked"));
        }
十三、判断行是否选中
 var len = $(".GridView_css").find("tr:gt(0) input[type='checkbox']:checked").length;
十四、分组全选
    function checkedGroup(obj) {
            $(obj).parent().parent().parent().find("input[type='checkbox']").attr("checked", $(obj).attr("checked"));
        }
十五、获取一个table中同一个表格中不同元素

          str[index][2] = tds.eq(5).find("span:first").html();
          str[index][3] = tds.eq(5).find("input:eq(0)").val();
          str[index][4] = tds.eq(5).find("input:eq(1)").val();

            $.each($("#grvCompont tr:gt(0)"), function (i, row) {
                var tds = $(row).find("td");
                if (tds.eq(0).find(":checkbox:checked").length > 0) {
                    str[index] = new Array();
                    //组件编号
                    str[index][0] = tds.eq(3).html();
                    //组件名称
                    str[index][1] = tds.eq(2).html();
                    //应用框架
                    str[index][2] = tds.eq(5).find("span:first").html();
                    //url路径
                    str[index][3] = tds.eq(5).find("input:eq(0)").val();
                    //动态库
                    str[index][4] = tds.eq(5).find("input:eq(1)").val();
                    //类名称
                    str[index][5] = tds.eq(5).find("input:eq(2)").val();
                    //组件id
                    str[index][6] = tds.eq(0).eq(0).find(":checkbox").attr("title");
                    //cs模态窗口
                    str[index][7] = tds.eq(5).find("input:eq(3)").val();
                    index++;
                }

            });

   
   foreach (string cookiename in Request.Cookies.AllKeys)
            {
                HttpCookie cookie = Request.Cookies[cookiename];
                Response.Write("<h4>" + cookie.Name + ":" + cookie.Value + "</h4><br/>");
            }
               {
                FrameBase.UserID = int.Parse(context.Request.QueryString["userid"].Trim());
                FrameBase.UserName = context.Request.QueryString["username"].Trim();
                FrameBase.UserRole = context.Request.QueryString["rolename"].Trim();
                FrameBase.FactoryCode = context.Request.QueryString["facoryCode"].Trim();

                string returnPage = context.Request.QueryString["ReturnPage"];
    
    
十六、RepeatColumns="" RepeatDirection="Horizontal" RepeatLayout="Table"
十七:判断元素是否为隐藏元素
     $("#ddlValue").is(":visible")
十八、
 table中取出大于除第一行外所有的tr
 var trCollections = $("#tableId tr:gt(0)");
 遍历其中所有的td
 trCollections.each(function(i){
  $(this).each(function){
   alert($(this).text());
  });
 });
十九、jQuery.unbind()出现不工作的解决办法
     原本以为$(...).unbind('click')可以取消绑定的'click'事件,谁知不起作用。后来查了查,原来$(...).unbind只能针对$(...).bind的事件,
  如果标签中直接写了onclick='...',只能用$(...).removeAttr('onclick')才能解决事件的绑定。


二十、大小写
     var stringValue = "hello world";
  toLocaleUpperCase()    //"HELLO WORLD"
  toUpperCase()          //"HELLO WORLD"
  toLocaleLowerCase()    //"hello world"
  toLowerCase()          //"hello world"
二十一、window.location
    例如: http://www.baidu.com:8080/windows/location/page.html?ver=1.0&id=timlq#love
    1, window.location.href
  返回值:http://www.baidu.com:8080/windows/location/page.html?ver=1.0&id=timlq#love
    2,window.location.protocol   
  URL 的协议部分
  返回值:http:   
 3,window.location.host   
  URL 的主机部分,   
  返回值:www.baidu.com   
 4,window.location.port   
  URL 的端口部分。如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符。   
  本例返回值:8080   
 5,window.location.pathname   
  URL 的路径部分(就是文件地址)   
  返回值:/windows/location/page.html   
 6,window.location.search   
  查询(参数)部分。除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值   
  返回值:?ver=1.0&id=timlq   
 7,window.location.hash   
  锚点   
  返回值:#love
二十二、刷新页面
     下面介绍全页面刷新方法:有时候可能会用到
  window.location.reload()刷新当前页面.
  parent.location.reload()刷新父亲对象(用于框架)
  opener.location.reload()刷新父窗口对象(用于单开窗口)
  top.location.reload()刷新最顶端对象(用于多开窗口)
二十三、jquery ajax
(function($) {
    $.fn.ajaxForm = function(success) {
        var form = $(this);
        var btn = form.find(":submit");
        form.submit(function() {
            $.ajax({
                url: form.attr("action"),
                type: form.attr("method"),
                data: form.serialize(),
                beforeSend: function(xhr) {
                    btn.attr("disabled", true);
                    showLoading();
                },
                success: function(data) {
                    if (success) { success(data); }
                },
                error: function() {
                    alert("请求出错,请重试");
                },
                complete: function() {
                    btn.attr("disabled", false);
                    hideLoading();
                }
            });
            return false;
        });
    };
    function showLoading() {
        $("#loading").css("display", "");
    };
    function hideLoading() {
        $("#loading").css("display", "none");
    };
})(jQuery);

二十四、jquery 的绑定方法 bind
       如果绑定的是函数名直接写函数名称,不用添加引号或者冒号
    function   DrpChange()
    {
  alert("test bingd");
    }
   
    正确:$("#ddlCommandType").bind("change", DrpChange); 
    错误:$("#ddlCommandType").bind("change", "DrpChange");  或者$("#ddlCommandType").bind("change", 'DrpChange');
二十五、jquery添加只读属性('disabled','disabled')
   $("input,textarea").each(function(){
 $(this).attr('disabled','disabled');
 });
 $("select").each(function(){
  $(this).attr('disabled','disabled');
 });
 //$(this).attr('readonly','readonly');  readonly属性有待测试,我还没有亲自测试过
二十六、Jquery取得iframe中元素的几种方法Javascript Jquery获取Iframe的元素、内容或者ID,反之也行!
 query取得iframe中元素的几种方法
 1、在iframe子页面获取父页面元素
   代码如下:
   $('#objId', parent.document);
  
 2、在父页面 获取iframe子页面的元素
   代码如下:
  $(document.getElementById['iframeId'].contentWindow.document.body).html()
  
 3、、根据iframename取得其中ID为"testId"元素
   $(window.frames["iframeName"].document).find("#testId").html()
   window.frames["iframeRad"].document 而不能使用 window.frames("iframeRad").document
 

原文地址:https://www.cnblogs.com/chenjt/p/3616652.html