Jquery中change方法里传递对象 但是只调用了一次?

新手上路,请多包涵

我想给一个下拉框赋值:

      <div class="input-control text full-size" data-role="input">
                            <select id="storageInfo" name="storageInfo">
                            </select>
                        </div>

Js:

 //采购change查询库房信息
                $("#purchaseEmp").bind("change", function () {
                    /**
                     * 查询采购员所对应的库房
                     */
                    $.ajax({
                        url: "/DemandPlan/Instock/SelectStorageWithEmp/",
                        dataType: 'JSON',
                        data: {
                            sys_emp_id: $("#purchaseEmp").val().split(',')[0]
                        },
                        type: 'POST',
                        success: function (resp) {
                            if (resp.res != "Failure") {
                                $("#storageInfo").empty();
                                if (resp.res.length == 0) {
                                    $("#storageInfo").append("<option value='none'>请配置库房...</option>");
                                }
                                $(resp.res).each(function (i, c) {
                                    $("#storageInfo").append("<option value='" + c.wl_storage_id + "," + c.wl_storage_name + "'>" + c.wl_storage_name + "</option>");
                                    // $("#UpdateDeptInfo").append("<option value='" + c.sys_dept_id + "," + c.dept_name + "'>" + c.dept_name + "</option>");
                                });
                                $("#storageInfo").select2();
                                // $("#UpdateDeptInfo").select2();
                            } else {
                                console.log(resp.Msg)
                            }
                        }
                    });
                });

这样写是没问题的,但是我声明了一个方法然后传入到里面,
图片描述

图片描述

这样写,下拉框的change事件只会执行一次,然后就没有效果了。
请告知为什么会这样?

阅读 2.4k
1 个回答

应该是:

$("#purchaseEmp").change(function() {
    // 函数调用
    selectPurchaseEmp(...);
});

如果 selectPurchaseEmp 没有参数的话,可以是:

$("#purchaseEmp").change(selectPurchaseEmp);

change() 的参数是一个函数

但你传入的 change(selectPurchaseEmp(...)) 不是selectPurchaseEmp这个函数,而是 selectPurchaseEmp(...)返回值

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题