首页 > js如何给几个点击button事件合并

js如何给几个点击button事件合并

如,我有几个好类似的button点击事件,这里怎么精简呢?

$("#search_next").click(function () {
$("#search_pre").click(function () {
$("#serach_one").click(function () {
$("#prePage").click(function () {
$("#nextPage").click(function () {
$(".jump").click(function () {

如果用switch的语句,那个条件怎么写呢?

switch(n)
{
case 1:
  执行代码块 1
break;
case 2:
  执行代码块 2
break;
default:
 n 与 case 1 和 case 2 不同时执行的代码
}

代码有点长,能看则看

 <script>


        function showDatas(data, tempBody) {

            for (var i = 0; i < data.length; i++) {

                tempBody.append("<tr><td>" + data[i].id + "</td><td>" + data[i].country + "</td><td>" + data[i].productionType + "</td><td>" + data[i].company + "</td><td>" + data[i].contactPerson + "</td><td>" + data[i].email + "</td><td>" + data[i].phone + "</td><td>" + data[i].fax + "</td><td>" + data[i].address + "<td align='center'><input type='checkbox' style='width: 20px;height: 20px;' name='yourBox' value=" + data[i].id + "></td>" + "</tr>");


            }

        }

        function IsNum(s) {
            if (s != null && s != "") {
                return !isNaN(s);
            }
            return false;
        }

        $(function () {


            //后一页
            $("#nextPage").click(function () {


                var nowPage = parseInt($(".currentPage").html());
                var alltempPage = parseInt($(".pageNum").html());

                if (nowPage <= alltempPage) {
                    var url = "askNextPage";
                    var args = {"curPage": nowPage + 1}

                    $.ajax({
                        url: url,
                        data: args,
                        dataType: "json",//返回类型
                        type: "POST",//请求方式
                        success: function (data) {
                            //请求成功时处理
                            // 设置当前页页码
                            var tempB = $(".common_body");
                            tempB.html("");

                            $(".currentPage").html(data["cur_page_num"]);
                            $(".pageNum").html(data["all_pages"]);

                            //请求成功时处理
                            showDatas(data["list"], tempB);
                        },

                        error: function () {
                            alert("发送异常...");

                        }
                    });


                }


            });


//            前一页
            $("#prePage").click(function () {

                var nowPage = parseInt($(".currentPage").html());

                if (nowPage > 1) {

                    var url = "askPrePage";
                    var args = {"curPage": nowPage - 1}

                    $.ajax({
                        url: url,
                        data: args,
                        dataType: "json",//返回类型
                        type: "POST",//请求方式
                        success: function (data) {
                            //请求成功时处理
                            // 设置当前页页码
                            var tempB = $(".common_body");
                            tempB.html("");

                            $(".currentPage").html(data["cur_page_num"]);
                            $(".pageNum").html(data["all_pages"]);

                            //请求成功时处理
                            showDatas(data["list"], tempB);
                        },

                        error: function () {
                            alert("发送异常...");

                        }
                    });

                }


            });

            //跳转查询
            $(".jump").click(function () {

                var jumpNum = $("#jumpPage").val();

                var alltempPage = parseInt($(".pageNum").html());

                if (jumpNum < 0) {

                    jumpNum = 1;
                    $("#jumpPage").val(1);

                } else if (jumpNum > alltempPage) {

                    jumpNum = alltempPage;
                    $("#jumpPage").val(alltempPage);

                } else {

                }

                if (IsNum(jumpNum)) {

                    var url = "askJumpPage";
                    var args = {"curPage": jumpNum}

                    $.ajax({
                        url: url,
                        data: args,
                        dataType: "json",//返回类型
                        type: "POST",//请求方式
                        success: function (data) {
                            //请求成功时处理
                            // 设置当前页页码
                            var tempB = $(".common_body");
                            tempB.html("");

                            $(".currentPage").html(data["cur_page_num"]);
                            $(".pageNum").html(data["all_pages"]);

                            //请求成功时处理
                            showDatas(data["list"], tempB);
                        },

                        error: function () {
                            alert("发送异常...");

                        }
                    });


                } else {

                    alert("请输入数字..");
                }

            });


            //显示
            $(".admin_icon").click(function () {


                var div = document.getElementById("man");
                div.style.display = "none";


                var uncle = document.getElementById("goBack");
                uncle.style.display = "block";
                var mother = document.getElementById("UserInfo");
                mother.style.display = "block";

            });

//退出
            $("#goBack").click(function () {

                var div = document.getElementById("man");
                div.style.display = "block";


                var uncle = document.getElementById("goBack");
                uncle.style.display = "none";
                var mother = document.getElementById("UserInfo");
                mother.style.display = "none";


            });

//查询
            $(".go_search").click(function () {

                var search_county = $("#search_name").val();
                if (search_county != "") {

                    var url = "searchByCounty";
                    var args = {"search_name": search_county, "show_page": 1};


                    $.ajax({
                        url: url,
                        data: args,
                        dataType: "json",//返回类型
                        type: "POST",//请求方式
                        success: function (data) {

                            var tempb = $(".search_body");
                            tempb.html("");

                            $(".search_currentPage").html(data["cur_page_num"]);
                            $(".search_pages").html(data["all_pages"]);


                            //请求成功时处理
                            showDatas(data["list"], tempb);
                        },
                        error: function () {


                            alert("请求异常!");

                        }
                    });

                } else {

                    alert("搜索内容不能为空");

                }

            });


//            查询下一页search_next

            $("#search_next").click(function () {

                var search_county = $("#search_name").val();
                var show_page = parseInt($(".search_currentPage").html()) + 1;
                var tempPage = parseInt($(".search_pages").html());
                if (search_county != "" && !show_page > tempPage) {

                    var url = "searchByCountyNextPage";

                    var args = {"search_name": search_county, "show_page": show_page};


                    $.ajax({
                        url: url,
                        data: args,
                        dataType: "json",//返回类型
                        type: "POST",//请求方式
                        success: function (data) {

                            var tempb = $(".search_body");
                            tempb.html("");

                            $(".search_currentPage").html(data["cur_page_num"]);
                            $(".search_pages").html(data["all_pages"]);


                            //请求成功时处理
                            showDatas(data["list"], tempb);
                        },
                        error: function () {


                            alert("请求异常!");

                        }
                    });

                } else {

                    alert("搜索内容不能为空或页码超出了");

                }

            });


//            查询上一页search_pre

            $("#search_pre").click(function () {

                var search_county = $("#search_name").val();
                var show_page = parseInt($(".search_currentPage").html()) - 1;

                if (search_county != "" && show_page != 0) {

                    var url = "searchByCountyPrePage";

                    var args = {"search_name": search_county, "show_page": show_page};


                    $.ajax({
                        url: url,
                        data: args,
                        dataType: "json",//返回类型
                        type: "POST",//请求方式
                        success: function (data) {

                            var tempb = $(".search_body");
                            tempb.html("");

                            $(".search_currentPage").html(data["cur_page_num"]);
                            $(".search_pages").html(data["all_pages"]);


                            //请求成功时处理
                            showDatas(data["list"], tempb);
                        },
                        error: function () {


                            alert("请求异常!");

                        }
                    });

                } else {

                    alert("搜索内容不能为空或者超出当前页码");

                }

            });


//            search_jump页码跳转
            $("#serach_one").click(function () {


                var search_county = $("#search_name").val();

                var show_page = parseInt($("#search_jump").val());


                if (search_county != "" && IsNum(show_page)) {


                    var url = "searchByCountyJumpPage";

                    var args = {"search_name": search_county, "show_page": show_page};


                    $.ajax({
                        url: url,
                        data: args,
                        dataType: "json",//返回类型
                        type: "POST",//请求方式
                        success: function (data) {


                            var tempb = $(".search_body");
                            tempb.html("");

                            $(".search_pages").html(data["all_pages"]);
                            $(".search_currentPage").html(data["cur_page_num"]);
                            //请求成功时处理
                            showDatas(data["list"], tempb);
                        },
                        error: function () {


                            alert("请求异常!");

                        }
                    });

                } else {

                    alert("搜索不能为空或页码不是数字");

                }

            });


            //弹出文本性提示框
            $(".goSearch").click(function () {
                $(".search_part").fadeIn();
            });

            //弹出:取消或关闭按钮
            $(".close_page").click(function () {

                $(".search_part").fadeOut();

            });


        });

    </script>

给你提供几种思路。
第一种:

var fun1=function(){};
var fun2=function(){};
var fun3=function(){};
var funs={
  'next':fun1,
  'prev':fun2,
  'other1':fun3
};
var handle=function(e){
  var $target=$(e.target);
  var fun=$target.attr('data-fun');

  //
  var handler=funs[fun];
  handler && handler();
}
//需要添加data-fun属性
$('[data-fun]').click(handle);

第二种:
使用你说的switch,我想你看了上面的代码之后,应该知道怎么处理case.


var arr = {
    search_next: function(){..},
    search_pre: function(){..},
    search_one: function(){..}
    .
    .
    .
}

for( name in arr){
    $("#" + name).click(arr[name]);
}

仅供参考


写一个方法,点击调用

【热门文章】
【热门文章】