2014-09-23 26 views
-1

我動態創建一個按鈕,並需要添加到L1標籤如何將按鈕添加到動態使用jQuery

LI標記這是我的jsfiddle

http://jsfiddle.net/1ty1v8u1/7/

enter image description here

這是我的代碼

function showRestaurantDetailsByLocation(response,locationname) 
{ 
$('.restListings').remove(); 
$('.addNewRestaurant').remove(); 
var ulhtml = $('<ul class="restListings"></ul>'); 
var divhtml = $('<div class="inner-intit"><sub class="sub">Your Favorite Area</sub></div>'); 
divhtml.append('<br>'); 
var $newbutton= $('<input/>').attr({ type: 'button', location:locationname , name:'btn1', class:'btn btn-success', value:'Add For'+locationname}); 
var $ordernow= $('<input/>').attr({ type: 'button',name:'btn1', class:'btn btn-success', value:'Order Now'}); 
for(var i=0;i<response.length;i++) 
{ 
divhtml.append('<li><h6>'+response[i].area+'</h6><p>'+response[i].address+'</p></li>'); 
} 
divhtml.append($newbutton); 
divhtml.append($ordernow); 
ulhtml.append(divhtml); 
$("#"+locationname).append(ulhtml); 
} 

我試着用這些選項

var $ordernow= $('<input/>').attr({ type: 'button', name:'btn2', class:'btn btn-success ordernow', value:'Order Now'}); 

divhtml.append('<li class="innerChild" user_location="'+locationname+'" vendor_name="'+response[i].vendor_name+'" vendor_id="'+response[i].vendor_id+'" ><h6> '+response[i].vendor_name+'</h6><p>'+response[i].locality+' , '+response[i].area+' </p><p><b>Timings:</b> '+response[i].start_time+' - '+response[i].end_time+'</p><span class="inDeleteInnerSub"></span></li>'); 

$('<li />').appendTo(ordernow); 

方法2:

divhtml.append('<li class="innerChild" user_location="'+locationname+'" vendor_name="'+response[i].vendor_name+'" vendor_id="'+response[i].vendor_id+'" ><h6> '+response[i].vendor_name+'</h6><p>'+response[i].locality+' , '+response[i].area+' </p><p><b>Timings:</b> '+response[i].start_time+' - '+response[i].end_time+'</p><span class="inDeleteInnerSub"></span>ordernow </li>'); 

方法3:

divhtml.append('<li class="innerChild" user_location="'+locationname+'" vendor_name="'+response[i].vendor_name+'" vendor_id="'+response[i].vendor_id+'" ><h6> '+response[i].vendor_name+'</h6><p>'+response[i].locality+' , '+response[i].area+' </p><p><b>Timings:</b> '+response[i].start_time+' - '+response[i].end_time+'</p><span class="inDeleteInnerSub"></span>'+$ordernow+'</li>'); 

有了這個它被顯示爲對象對象

+0

需要更多的努力來解釋您的問題。你沒有提供對問題的解釋,或者主題中的預期行爲和問題是唯一提出的問題,而且非常含糊。 – charlietfl 2014-09-23 23:19:44

+1

那麼它不是我沒有試過,我在本地嘗試,並不想要把這是網絡的代碼。無論如何,我會用我試過的東西更新我的問題 – Pawan 2014-09-23 23:21:24

回答

1

如果你只是想要你的「立即訂購」按鈕在裏面NT更改以下行:

原文:

divhtml.append($ordernow); 

修改:

divhtml.children("li").append($ordernow); 

此外,如果你希望你的按鈕出現在右邊眼前這個CSS屬性添加到該按鈕:

float:right 

這裏是更新的JSFiddle

編輯:

甚至接近你的照片是這樣的JSFiddle。我添加了h6和p的容器div,其中css屬性爲

float:left 
+0

非常感謝,這對我來說幫助很大。 – Pawan 2014-09-24 09:30:33