gpt4 book ai didi

Javascript 函数参数出现意外结果

转载 作者:行者123 更新时间:2023-12-03 11:03:24 25 4
gpt4 key购买 nike

我有一个简单的 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();
}

用于我的数据网格和 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>

最佳答案

如果您的函数调用参数包含在逗号分隔的列表中,从您的评论来看,您可以使用 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));
}
});

关于Javascript 函数参数出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com