javascript
  • jquery
  • jquery-plugins
  • 2012-07-18 61 views 1 likes 
    1

    我有一個小問題:我需要將字符串的值從函數A傳遞給函數B,函數B將它作爲參數並使用它。如何從jQuery中的函數返回一個字符串作爲其他jQuery函數的參數?

    我試過以下,但它不工作。

    // 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() + '" , "commissionAmount": "' + $("input#commissionAmount").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); 
          } 
         }); 
        }; 
    

    回答

    4

    我不知道,但你不執行功能,試試這個:

    alert(x()); 
    $.B(x()); 
    
    0

    而不是

    $.B(x); 
    

    調用X()和B()直線距離:

    B(x()); 
    


    爲了能夠使用 B()像這樣: $.B()您需要將功能添加到jQuery的 $

    $.B = function(){... 
    

    jQuery.B = function(){... 
    

    但它一般不推薦污染$ - 這是更好的使用你自己的名字空間。

    在這裏看到:is-it-possible-to-create-a-namespace-in-jquery

    +0

    我只是做了以下內容,它爲我工作 '$( 「#一個SayHello的」)點擊(函數(E){ e.preventDefault(); 變種X = A(); // alert(x); B(x); }); //第一個函數(A) 函數A(){0} {「ProductID」:「'+ $(」input#ProductID「).val()+'」,「AffiliationURL」:「' +(「input#AffiliationURL」)。val()+'「,」Quantitiy「:」'+ $(「input#Quantitiy」)。val()+'「,」commissionAmount「:」'+ $(「 input#commissionAmount「)。val()+'」}'; return dt.toString(); };' – 2012-07-18 12:26:30

    0

    嘗試調用B功能沒有,只是B(),就像任何簡單的功能 「$」。

    此外,您可以在B()函數內部添加一個alert(dt),以檢查參數中的數據。

    相關問題