我試圖創建一個簡單的組合框,但也許jQuery的整合邏輯可能是不正確的,我想知道如果有辦法做到這一點,我可能不會看到一個更合乎邏輯的方式。動態添加元素和處理事件
我有這樣的代碼,填充基於PHP的拉動從MySQL的一個下拉菜單:
這基本上有一個下拉菜單,depnding在選擇的價值,上展開將拉動其他選擇框,是從一個PHP腳本和MySQL查詢
$("#expandBtn").click(function(){
i++;
var checkSelect = $("#levelDrop option:selected").val();
if(checkSelect !== "NULL"){
var itemSelected = $("#levelDrop option:selected").val();
alert(itemSelected);
$.ajax({ url: '****/phpFunctions/actions/getSelectList.php',
data: {w: itemSelected, x: i},
type: 'GET',
success: function(output) {
alert(output);
$("#expandBtn").remove();
$("#levelsRow").append(output);
}
});
}//end if check select
//send the variable to getFeatures list and have it reuturn in the divison and print the value in application.
});
php的輸出端的查詢結果具有完全相同的字段和字段名。這應該適用於X數量的選擇和擴展,唯一的問題是該按鈕在第二次輸入後將不起作用。
看到這些職位信息如何使用委派事件處理動態地添加元素:http://stackoverflow.com/questions/8752321/jquery-live-vs-on-method-for-adding-a-click-事件後裝動態-HT/8752376#8752376和http://stackoverflow.com/questions/9814298/does-jquery-on-work-for-elements-that-are-added-after-the-event-處理程序,是-CRE/9814409#9814409。 – jfriend00