2015-03-25 41 views
0

在appendTo似乎並沒有工作類型文本框的情況。(爲什麼?) 追加代碼工作其他地方無法使用append/appendto/innerhtml或任何其他向父div添加輸入框。

if($('#'+json.id).length) 

也是如此,但在HTML顯示不出來。 ㈣一直在這兩天:(

這裏的

<body> 
< div id = "page" > 
    < div id = "id1" > hello world < input id = "LAPD" > < /div> 
< /div> 
< /body> 

與ID輸入= 「LAPD」 從生成的我的js代碼

switch (json.type) { 

    case "container": 
     $('<div id=' + json.id + '/>').appendTo('#' + parent_id); 
     $('#' + json.id).css({ 
      // container styling 
     }); 
     break; 

    case "text": 
     $('#' + parent_id).text(json.value); 
     var j = document.createElement('input'); 
     j.id = "LAPD"; 
     j.value = "LAPD" 
     $(j).appendTo("#"+parent_id); 
     break; 

    case "image": 
     $('<img id=' + json.id + ' src=' + json.value + '/>').appendTo('#' + parent_id); 
     break; 

    case "textbox": 
     var j = document.createElement('input'); 
     j.id = json.id; 
     j.type = 'text'; 
     $(j).appendTo("#"+parent_id); 
     if($('#'+json.id).length){ 
      console.log("exists"); 
      } 
     else{ 
      console.log("doesnt"); 
      } 
     break; 
} 

生成的HTML相關的部分案例:「文字」

+0

您需要創建一個[MCVE](/ help/mcve)。基本上,上述作品的相關部分:http://jsbin.com/semetezewu/1 – 2015-03-25 08:24:49

+0

看起來不錯 - https://jsfiddle.net/arunpjohny/L9wovxxt/1/ - 看看你的CSS規則 – 2015-03-25 08:26:21

+0

謝謝你的MCVE擡頭!新人在這裏。但它不適合我。任何想法爲什麼? (同時在mcve上工作) – user2889561 2015-03-25 09:27:34

回答

0

我發現一些調試後的問題。輸入框確實附加到HTML,但問題出現由於代碼添加文本

$('#' + parent_id).text(json.value); 

這是用提供的文本替換父div的整個內容,而不是將其附加到當前內容,正如我所預期的那樣。

相關問題