具有意外结果的Javascript函数参数

时间:2015-01-15 21:21:58

标签: javascript jquery

我有一个简单的javascript函数,可以打开一个模态对话框。如果我传递一些参数,它将打开没有问题。我正在使用jQuery在我的网站上启用一些键盘功能。基本上,用户将导航一堆表格单元格,找到他们正在寻找的值并点击“Enter”。我有一个等待keyCode 13的事件监听器。当它触发时,我从该单元格中获取HTML,通过正则表达式运行它并拉出一个字符串。我把那个字符串传递给我的javascript函数。

如果我将值硬连接到函数中它可以正常工作,如果我传递它从正则表达式返回的字符串它总是失败。如果我将硬连线值与正则表达式的结果进行比较,则它们是100%相同的。我无法理解为什么函数不会从正则表达式中返回变量。

Javascript功能:

function openDetail(event, newWindow, className, methodName, param, param1, param2) {

            param = (typeof param === "undefined") ? "defaultValue" : param;
            param1 = (typeof param1 === "undefined") ? "defaultValue" : param1;
            param2 = (typeof param2 === "undefined") ? "defaultValue" : param2;
            newWindow = (typeof newWindow === "undefined") ? "defaultValue" : newWindow;

            var windowName = '';
            if (newWindow == true) {
                windowName = '#openExtendedDetailWindow';
            } else {
                windowName = '#openDetailWindow';
            }

            param = param.trim();
            param1 = param1.trim();
            param2 = param2.trim();
            var page = "?url=" + className + "/" + methodName + "/" + param + "/" + param1 + "/" + param2;

            $(windowName).dialog({
                width: 'auto',
                height: 'auto',
                resizable: true,
                modal: true,
                position: 'center',
                open: function (event, ui) {
                    $(this).load(page, function () {
                    });
                },
                show: {
                    effect: 'fade',
                    duration: 800
                }
            });
            event.preventDefault();
        }

我的datagrid和keydown事件监听器的Javascript:

$(document).ready(function () {

        $('#warehouseSelection').change(function (event) {
            var warehouse = $('#warehouseSelection').val();
            window.location.replace("?url=FHInventory/index/" + warehouse);
            event.preventDefault();
        });

      var table = $('#contentTable').DataTable({

            "paging": false,
         "jQueryUI": true,
          "scrollX": true,
         columnDefs: [
         {
         targets: [ 0 ],
         orderData: [ 0, 1 ]
         },
         {
         targets: [ 1 ],
         orderData: [ 1, 0 ]
         },
         {
         targets: [ 4 ],
         orderData: [ 4, 0 ]
         }
         ]
        });
       $keys = new $.fn.dataTable.KeyTable( table );

        $("body").on("keydown",$("form.form-horizontal"), function (e) {

            if (e.keyCode == 13) {

                $url = $keys.fnGetCurrentData();
                alert($url);
                var regExp = /\(([^)]+)\)/;
                var matches = regExp.exec($url);
                $params = matches[1];
                alert($params);
                openDetail(String($params));
            }
        });

    });

硬连线HTML(可行),

<td>
<a class="masterTooltip" href="" title="Received" onclick="openDetail(event,false,'Inventory','receivedSpecificDate','27934','RADCH6LC0081', '20150117')">0</a>
</td>

1 个答案:

答案 0 :(得分:1)

如果您的逗号分隔列表中包含函数调用的参数,您可以使用split将其转换为数组,splice在开头添加事件对象,然后apply

$("body").on("keydown",$("form.form-horizontal"), function (e) {

    if (e.keyCode == 13) {

        $url = $keys.fnGetCurrentData();
        alert($url);
        var regExp = /\(([^)]+)\)/;
        var matches = regExp.exec($url);
        $params = matches[1];
        alert($params);

        //this assumes that none of your parameters actually have a comma in
        //if they do, you'll need to split up the string into an array 
        //in a different manner
        //It also splices in the current event (e) to the first parameter
        openDetail.apply(this, $params.split(',').splice(0,0,e));
    }
});