请教一下,如何将js函数作为另一函数的参数调用,被调用的函数是带参数的
在Receive()中执行PostUrl()函数,在执行PostUrl函数时,将contents(urls)函数作为PostUrl函的参数传入,作为回调函数使用,但con...
在Receive()中执行PostUrl()函数,在执行PostUrl函数时,将contents(urls)函数作为PostUrl函的参数传入,作为回调函数使用,但contents()函数是带参数的,如何将参数一并传入呢?主要代码如下:
<script>
function Receive() {
var action = "Receive";
var URL = "/ControlUnit/Menu/sms_submit.ashx";
var finterid = 9702;
var ftrantype = 301;
var fuserid = 3;
var urls = "/ControlUnit/Menu/MessageShow.aspx";
PostUrl(URL, action, finterid, ftrantype, fuserid, fn(urls));
//说明,这里fn(urls)作为参数传入是有问题的,因为它已经被执行了,而不是作为参数传入。
}
var fn = function contents(urls) {
if (urls != "") {
var userAgent = navigator.userAgent.toLowerCase();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
if (is_moz) {
//window.opener.parent.frames['iframeright'].document.location.href = urls;
}
else {
var pWindow = window.dialogArguments;
//window.dialogArguments.top.iframeright.location.href = urls;
}
window.close();
}
};
function PostUrl(URL, action, finterid, ftantypeid, fuserid, fn) {
var datastr = "{'action':'" + action
+ "','FUserID':'" + fuserid
+ "','FInterID':'" + finterid
+ "','FTranType':'" + ftantypeid
+ "'}";
var datajson = eval("(" + datastr + ")");
//var datajson = datastr;
$.ajax({
type: "Post",
url: URL,
data: datajson,
//contentType: "application/json",
dataType: "Json",
success: function (data) {
fn(urls);
error: function (err) {
alert(err);
}
});
}
<html>
<body>
<div>
<input type = "text" onclick= "Receive();" />
</div>
</body>
</html> 展开
<script>
function Receive() {
var action = "Receive";
var URL = "/ControlUnit/Menu/sms_submit.ashx";
var finterid = 9702;
var ftrantype = 301;
var fuserid = 3;
var urls = "/ControlUnit/Menu/MessageShow.aspx";
PostUrl(URL, action, finterid, ftrantype, fuserid, fn(urls));
//说明,这里fn(urls)作为参数传入是有问题的,因为它已经被执行了,而不是作为参数传入。
}
var fn = function contents(urls) {
if (urls != "") {
var userAgent = navigator.userAgent.toLowerCase();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
if (is_moz) {
//window.opener.parent.frames['iframeright'].document.location.href = urls;
}
else {
var pWindow = window.dialogArguments;
//window.dialogArguments.top.iframeright.location.href = urls;
}
window.close();
}
};
function PostUrl(URL, action, finterid, ftantypeid, fuserid, fn) {
var datastr = "{'action':'" + action
+ "','FUserID':'" + fuserid
+ "','FInterID':'" + finterid
+ "','FTranType':'" + ftantypeid
+ "'}";
var datajson = eval("(" + datastr + ")");
//var datajson = datastr;
$.ajax({
type: "Post",
url: URL,
data: datajson,
//contentType: "application/json",
dataType: "Json",
success: function (data) {
fn(urls);
error: function (err) {
alert(err);
}
});
}
<html>
<body>
<div>
<input type = "text" onclick= "Receive();" />
</div>
</body>
</html> 展开
展开全部
PostUrl(URL, action, finterid, ftrantype, fuserid, fn,urls);
function PostUrl(URL, action, finterid, ftantypeid, fuserid, fn,urls) {
fn(urls);
}
追问
urls也是作为单独的参数调用啊,那fn万一有很多个参数,不是得长长的一串了吗?有没有更好的办法啊
追答
PostUrl(URL, action, finterid, ftrantype, fuserid, fn,urls);
function PostUrl(URL, action, finterid, ftantypeid, fuserid, fn,urls) {
fn(urls);
}
function fn(urls) {
alert(urls[0]);
alert(urls[1]);
...
alert(urls[n]);
}
function fn2(obj) {
alert(obj.prop1);
alert(obj.prop2);
...
alert(obj.propN);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询