最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to return a string from a function in jQuery as a parameter to the other jQuery function? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 4

I'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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论