2012-07-14 52 views
0

我的代碼中有2個函數 - makeData和displayData - 。通過jquery.html將數據傳遞給javascript函數()

<script type="text/javascript"> 
    function makeData(){ 
     var myjson = {}; var myarray = []; 
     for (var i = 0; i < 10; i++){ 
      myjson[i] = "json"+i; 
      myarray[i] = "array"+i; 
     } 

     //when i pass myjson[1] and myarray[1] value, it's work for me 
     $("#mydiv").html("<input type=\"button\" value=\"display data\" 
       onclick=displayData(\""+myjson[0]+"\",\""+myarray[0]+"\") />"); 
     /*but when i tried to pass the json and array object, i got problem here. 
      displayData() function cannot read those objects*/ 
     $("#mydiv").html("<input type=\"button\" value=\"display data\" 
       onclick=displayData(\""+myjson+"\",\""+myarray+"\") />"); 

    } 
</script> 
<input type="button" value="make some button" onclick="makeData()" /> 
<div id="mydiv"></div> 
<div id="dataDisplay"></div> 

如何將數組/ json對象傳遞給使用innerHTML編寫的javascript函數?

注意:我的目的是將數據傳遞給javascript函數。

編輯:我想現在它現在更清楚了。對不起之前..

+0

你應該重構你的整個代碼,不要混用jQuery和蹩腳的內聯事件,直接訪問由jQuery以更好的方式提供的東西('window.event'是IE-only!) – ThiefMaster 2012-07-14 09:27:38

+0

哦,對不起我的無序代碼..但我仍然是新的JavaScript和jQuery。所以現在什麼方法適用於我,我寫了什麼。我希望我可以幫助改善。 – Ahok 2012-07-14 09:36:30

+0

我仍然沒有得到你想要達到的目標。 – m02ph3u5 2012-07-14 09:40:58

回答

0

我想我已經找到了我的問題的答案。這是修改後的代碼:

function buatdata(){ 
    var myjson={}; 
    var myarray= []; 
    for(var i = 0 ; i < 10 ; i++){ 
     myjson[i] = "json"+i; 
     myarray[i] = "array"+i; 
    } 
    var arraystring = myarray.toString().replace(\,\g,"&#44;"); 
    var jsonstring = JSON.stringify(myjson).replace(\"\g,"&quot;"); 

    /* i changed " to ' to wrap parameter that i passed in tampildata() function 
     and i convert those object to string first.*/ 

    $("#tombol").html("<input type=\"button\" value=\"display\" 
      onclick=tampildata('"+jsonstring+"','"+arraystring+"') />"); 
} 
function tampildata(getjson,getarray){ 
    //i changed string that i passed back to object, so i get what i want. 
    var myarray = getarray.split(','); 
    var myjson = JSON.parse(getjson); 
} 

使用該函數,我傳遞數組和json對象。但我仍然希望有其他方法可以使用。因爲我害怕如果我的數據集非常大,它可以減緩散文。

+1

好,不是這是我的答案... – m02ph3u5 2012-07-14 15:47:05

+0

好吧,現在我發現了一個新問題,我的答案上面..如果json /數組值有空間例如myjson [i] =「試一試」或myarray [i] =「這個值也有空格」)。它不起作用.. – Ahok 2012-07-15 03:13:57

+0

你爲什麼不把你的價值存儲在全球? – m02ph3u5 2012-07-16 12:23:53

1

您可以將您的對象或數組轉換爲字符串表示(JSON),將它寫入DOM(例如,作爲您調用onclick函數的參數)。

給我10分鐘寫一個更好的例子。 這是。這並不完美(例如,即使實際上是「數字」,索引似乎也是「字符串」類型的),但它應該覆蓋大多數情況。請注意,您不能使用此示例轉換函數或引用:

function convertObjectToString(obj) 
{ 
    var stringRep = "{"; 

    for (var index in obj) 
    {  
     var cIndex; 
     if (typeof index == "number") // int index 
      cIndex = index;  
     else // string index 
      cIndex = "\"" + index + "\""; 

     if (typeof obj[index] == "object") 
      stringRep += cIndex + ":" + convertObjectToString( 
          obj[index]) + ","; // convert recursively 
     else if (typeof obj[index] == "number") 
      stringRep += cIndex + ":" + obj[index] + ","; 
     else 
      stringRep += cIndex + ":\"" + obj[index] + "\","; 

    } 
    // remove trailing comma (if not empty) 
    if (stringRep.length > 1) 
     stringRep = stringRep.substr(0, stringRep.length - 1); 
    stringRep += "}"; 
    return stringRep; 
} 

或者更好的只是使用JSON.stringify函數! https://developer.mozilla.org/En/Using_native_JSON/

+0

所以,我需要添加基於您的例如JSON.parse(stringRep);對?我仍然沒有得到它..但讓我試試你的建議.. – Ahok 2012-07-14 10:53:53

+0

hhmmm ...好吧..現在我主要了解json。但是,我仍然無法將json對象傳遞給displayData(),在上面寫的html輸入腳本中。 – Ahok 2012-07-14 12:16:00

+0

嗯,我不知道你的displayData函數是做什麼的。無論如何,您必須使用displayData中的parseJson將字符串重新轉換爲對象。 – m02ph3u5 2012-07-14 13:02:37

相關問題