admin管理员组文章数量:1421182
I have a small issue: I need to pass the value of string from function A to function B which will take it as argument and use it.
I have tried the following but its not working.
// first function (A)
$("a#SayHello").click(function (e) {
e.preventDefault();
var x = function () {
var dt = '{"ProductID": "' + $("input#ProductID").val() + '" , "AffiliationURL": "' + $("input#AffiliationURL").val() + '" , "Quantitiy": "' + $("input#Quantitiy").val() + '" , "PricePerUnit": "' + $("input#PricePerUnit").val() + '" , "missionAmount": "' + $("input#missionAmount").val() + '"}';
return dt.toString();
};
alert(x);
$.B(x);
});
// second function
function B(dt) {
$.ajax({
type: 'POST',
data: dt,
url: 'http://localhost:4528/WebSite1/WebService.asmx/InsertCommissionRecord',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, textStatus, XMLHttpRequest) {
alert(data.d);
alert(XMLHttpRequest);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
};
I have a small issue: I need to pass the value of string from function A to function B which will take it as argument and use it.
I have tried the following but its not working.
// first function (A)
$("a#SayHello").click(function (e) {
e.preventDefault();
var x = function () {
var dt = '{"ProductID": "' + $("input#ProductID").val() + '" , "AffiliationURL": "' + $("input#AffiliationURL").val() + '" , "Quantitiy": "' + $("input#Quantitiy").val() + '" , "PricePerUnit": "' + $("input#PricePerUnit").val() + '" , "missionAmount": "' + $("input#missionAmount").val() + '"}';
return dt.toString();
};
alert(x);
$.B(x);
});
// second function
function B(dt) {
$.ajax({
type: 'POST',
data: dt,
url: 'http://localhost:4528/WebSite1/WebService.asmx/InsertCommissionRecord',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, textStatus, XMLHttpRequest) {
alert(data.d);
alert(XMLHttpRequest);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
};
Share
Improve this question
edited Jul 18, 2012 at 11:28
Otto Allmendinger
28.4k7 gold badges71 silver badges79 bronze badges
asked Jul 18, 2012 at 11:25
SoftBuilders PkSoftBuilders Pk
1094 silver badges15 bronze badges
3 Answers
Reset to default 4I'm not sure but your not executing the function, try this:
alert(x());
$.B(x());
Try call the B function without " $. ", just B()
, like any simple function.
Also, you can add an alert(dt)
inside B() function, to check the data from parameter.
Instead of
$.B(x);
call x() and B() straight away:
B(x());
To be able to use
B()
like this: $.B()
you need to add your function to jQuery's $
:
$.B = function(){...
or
jQuery.B = function(){...
But it's generally not remended to pollute $
- it's much better to use your own namespace.
See here: is-it-possible-to-create-a-namespace-in-jquery
本文标签:
版权声明:本文标题:javascript - how to return a string from a function in jQuery as a parameter to the other jQuery function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745348880a2654641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论