2012-06-01 159 views
2

我試圖在點擊javascript函數中傳遞兩個參數。我需要用引號括起參數。當我把它放在單引號中時,它會打破從圖像開始的引用,當我使用double時,它會打破javascript前面的引號。我如何附上它,它不會破壞我的代碼?使用jquery追加javascript onclick事件中的代碼追加

$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction('addnew',$("#field").val())"/> Error.') 

回答

1

你可以這樣做:

$("#Div").append($("<img>", { 

    id: "myImage", 

    src: "/images/myimage.png", 

    click: function() { 
     myfunction('addnew', $("#field").val()); 
    } 

}), " Error."); 
+0

我想上的跨度標籤和單擊事件不起作用使用此代碼。我的代碼: $( 「#錯誤」)追加($( 「」,{ \t \t \t \t \t \t \t \t \t \t \t \t ID: 「編輯」, \t \t \t \t \t \t \t \t \t \t \t \t title:action, \t \t \t \t \t \t \t \t \t \t \t \t點擊:函數(){ \t \t \t \t \t \t \t \t \t \t \t \t \t編輯(動作,$( '#字段')VAL()); \t \t \t \t \t \t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t \t \t \t \t \t}),ERRMSG); – user829982

+0

@ user829982很可能是'Edit'部分不工作,放''click:function(){console.log(「test」);}'來測試click事件是否不起作用。 – Esailija

+0

只有當我使用img標籤時,控制檯纔會添加測試。它似乎並沒有編輯功能不起作用 – user829982

1

試試這個,你需要\逃脫撇號:

$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction(\'addnew\',$("#field").val())"/> Error.') 
2

你將不得不使用反斜線引號,因爲這樣的:

$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction(\'addnew\',$("#field").val())"/> Error.') 
0

逃離單引號大約\'addnew\'

$("#Div").append('<img id ="myImage" src ="/images/myimage.png" onclick="javascript:myfunction(\'addnew\',$("#field").val())"/> Error.')