JavaScript随记

jQuery 选择器

使用列标题定位并搜索

公用js

common.js

function input_text_addSearchPre(modeId, targetId, selectId, valueId) {
    $("#" + valueId).attr("onkeydown", "if(event.keyCode==13){event.keyCode=0;event.returnValue=false;input_text_addSearchPost('" + modeId + "','" + targetId + "','" + selectId + "','" + valueId + "');}")
}
function input_text_addSearchPost(modeId, targetId, selectId, valueId) {
    var model = $("#" + modeId + " option:selected").val();
    var colPo = $("#" + selectId + " option:selected").val();
    var value = $("#" + valueId).val();
    var tds = $("#" + targetId + " tr").find("td:eq(" + colPo + ")");
    $.each(tds, function() {
        if (model == 0) {
            if ($(this).text().indexOf(value) >= 0) {
                $(this).parent("tr").show();
            } else {
                $(this).parent("tr").hide();
            }
        } else if (model == 1) {
            if ($(this).text().indexOf(value) < 0) {
                $(this).parent("tr").hide();
            }
        }
    });
}
View Code

页面js

<script type="text/javascript">
    function select_modeSelect_onmouseover() {
        $("#select_modeSelect option[value='temp']").remove();
    }
    function select_selectColPo_onmouseover() {
        var tds = $("#thead_userRepository td");
        $("#select_selectColPo option[value='temp']").remove();
        for (var i = 0; $("#select_selectColPo option").length < tds.length && i < tds.length; i++) {
            $("#select_selectColPo").append("<option value=\""+i+"\">" + $("#thead_userRepository td").eq(i).text() + "</option>");
        }
    }
    $(document).ready(function() {
        input_text_addSearchPre("select_modeSelect", "tbody_userRepository", "select_selectColPo", "input_text_searchValue");
    })
</script>
View Code

参考用例

   <div id="div_tools">
      <select
         id="select_modeSelect"
         style="width: 100px; height: 100%;"
         onmouseover="select_modeSelect_onmouseover()">
         <option value="temp">模式</option>
         <option value="0">平行</option>
         <option value="1">递进</option>
      </select>
      <select
         id="select_selectColPo"
         style="width: 100px; height: 100%; margin-left: 0px;"
         onmouseover="select_selectColPo_onmouseover()">
         <option value="temp">位置</option>
      </select>
      <input
         id="input_text_searchValue"
         type="text"
         style="width: 200px; height: 21px; margin-left: 0px;" />
   </div>
   <div id="div_includeTable">
      <table style="border-collapse: collapse; width: 100%;">
         <thead id="thead_userRepository">
......
View Code

使用列标题定位并排序

页面js

<script type="text/javascript">
    function thead_tr_td_onclick(colPo) {
        var tds = $(".tr_tbody_userRepository").find("td:eq(" + colPo + ")");
        var tdA = $(tds).eq(0).text().trim();
        var tdB = $(tds).eq(1).text().trim();
        var ascORdesc;
        if (tdA.length > tdB.length) {
            ascORdesc = 1;
        } else if (tdA.length < tdB.length) {
            ascORdesc = -1;
        } else if (tdA > tdB) {
            ascORdesc = 1;
        } else {
            ascORdesc = -1;
        }
        tbody_userRepository_sort(colPo, ascORdesc);
    }
    function tbody_userRepository_sort(colPo, ascORdesc) {
        var trs = $("#tbody_userRepository tr");
        trs.sort(function(a, b) {
            var tdA = $(a).children("td").eq(colPo).text().trim();
            var tdB = $(b).children("td").eq(colPo).text().trim();
            if (tdA.length > tdB.length) {
                return ascORdesc;
            } else if (tdA.length < tdB.length) {
                return ascORdesc * (-1);
            } else if (tdA.length == tdB.length) {
                return (tdA > tdB) ? ascORdesc : ascORdesc * (-1);
            }
        })
        $("#tbody_userRepository").append(trs);
        for (var i = 0; i < $(".td_userRepository_indexCol").length; i++) {
            $(".td_userRepository_indexCol").eq(i).text(i + 1);
        }
    }
</script>
View Code

参考用例关键代码

         <thead id="thead_userRepository">
            <tr>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('0')">序号</td>
               <td
                  style="width: 400px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('1')">用户ID</td>
               <td
                  style="width: 200px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('2')">联系方式</td>
               <td
                  style="cursor: pointer;"
                  onclick="thead_tr_td_onclick('3')">地址</td>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('4')">是否欠费</td>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('5')">动作</td>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('6')">阀状态</td>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('7')">余额</td>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('8')">应交费用</td>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('9')">数额</td>
               <td
                  style="width: 100px; cursor: pointer;"
                  onclick="thead_tr_td_onclick('10')">操作</td>
            </tr>
         </thead>
View Code

Ajax异步请求

返回Json对象

关键部分

            var myData = "user.phoneCall=" + userP + "&user.instructions=" + instructions + "&account.customId=" + accountCid + "&infs['rechargeValue']=" + rechargeValue;
            $.ajax({
                url : "/ChargingSystem/admin/recharge.do",
                data : myData,
                type : "POST",
                async : true,
                cache : false,
                processData : true,
                dataType : "JSON",
                success : function(user) {
                    console.log(user);
                    $("#td_arrears_" + trIndex).text(user.arrears == "0" ? "否" : "是");
                    $("#input_button_instructions_" + trIndex).val(user.instructions == "0" ? "关闭" : "开启");
                    $("#td_valve_" + trIndex).text(user.valve == "0" ? "关闭" : "开启");
                    $("#td_balance_" + trIndex).text(user.account.balance);
                    $("#td_due_" + trIndex).text(user.account.due);
                    alert("成功");
                },
                error : function() {
                    alert("失败");
                }
            })
View Code

返回文档对象

关键部分

            var form_userInfANDaccountInf = new FormData($("#form_userInfANDaccountInf")[0]);
            var myUrl = parseInt("${operator.jurisdiction}") >= 2 ? "/ChargingSystem/admin/delete.do" : "/ChargingSystem/user/delete.do";
            $.ajax({
                url : myUrl,
                type : "POST",
                data : form_userInfANDaccountInf,
                async : true,
                cache : false,
                contentType : false,
                processData : false,
                success : function(user) {
                    console.log(user);
                    if (user == "myTrue") {
                        alert("已删除,即将离开此页");
                        window.close();
                    } else {
                        alert("删除失败");
                    }
                }
            })
View Code

跨域访问

<script type="text/javascript">
    function getBookmark() {
        $.ajax({
            url : "......",
            type : "GET",
            async : true,
            cache : false,
            dataType: 'jsonp',
            crossDomain: true,
            success : function(result) {
                alert(result);
            }
        })
    }
View Code

外部JS库

<script
   type="text/javascript"
   src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
View Code

猜你喜欢

转载自www.cnblogs.com/suheng/p/9296179.html
今日推荐