Javascript 使用jQuery时如何将控件传递给函数?
“#SelectView”是一个Select控件,代码a运行良好,我希望能编写一个函数来实现它 但是方法1和方法2都失败了,为什么?我该怎么写这封信 代码AJavascript 使用jQuery时如何将控件传递给函数?,javascript,jquery,Javascript,Jquery,“#SelectView”是一个Select控件,代码a运行良好,我希望能编写一个函数来实现它 但是方法1和方法2都失败了,为什么?我该怎么写这封信 代码A $("#SelectView").change(function () { temp.view = $("#SelectView").get(0).selectedIndex; window.location.href = CreateJumpLink(temp); }); 方法1 SetJumpForSelectChan
$("#SelectView").change(function () {
temp.view = $("#SelectView").get(0).selectedIndex;
window.location.href = CreateJumpLink(temp);
});
方法1
SetJumpForSelectChanged('#SelectView', temp.view);
function SetJumpForSelectChanged(name, par) {
$(name).change(function () {
par = $(name).get(0).selectedIndex;
window.location.href = CreateJumpLink(temp);
});
}
方法2
SetJumpForSelectChanged( $('#SelectView'), temp.view);
function SetJumpForSelectChanged(name, par) {
name.change(function () {
par = $(name).get(0).selectedIndex;
window.location.href = CreateJumpLink(temp);
});
}
已添加
function ClassURLPar() {
this.filename;
this.diskindex=0;
this.diskcount=1;
this.imagefilter = 0;
this.sortby = 0;
this.path = "/";
this.view = 0;
}
var temp = jQuery.extend(true, {}, mURL);
在JavaScript中,当传递原语(如字符串)时,不能通过引用传递。因此,在第一个函数中,您传入
temp.view
,它在函数中作为par
存在。您可以修改par
,但不使用它,而是将原始temp
传递给CreateJumpLink
令人困惑的是(如果您是JS新手),当您的参数是对象时,它是通过引用传递的
SetJumpForSelectChanged('#SelectView', temp);
function SetJumpForSelectChanged(name, tempReference) {
$(name).change(function () {
tempReference.view = $(name).get(0).selectedIndex;
window.location.href = CreateJumpLink(tempReference); // or temp, same result if it's in scope
});
}
如果
temp
在这两个地方都在范围内,那么您根本不需要传入它。您引用的是什么temp您不应该重新分配参数中的var
s。您不能使用par
执行任何操作。什么是CreateJumpLink(temp)
?请查看我添加的