2016-08-19 33 views
0

我有以下的HTML標記。在dropdown更改select element (project_dp),那麼我提取父div。並獲得parent div後,我提取parent divnext div。之後,當我嘗試使用find方法獲得select element(task)存在於這個div內,那麼我什麼都沒有。無法找到div中使用jquery的select元素?

<div class="div_cell"> 
    <select class="form-control project_dp" id="log_time_project_id" name="log_time[project_id]" onchange="populate_tasks($(this))"> 
     <option value=""></option> 
     <option disabled="true" selected="selected">Please Select</option> 
     <option value="18">akjsda</option> 
     <option value="19">approval process and approvers save test</option> 
     <option value="39">billable test</option></select> 
</div> 
<div class="div_cell"> 
    <select class="form-control task" id="log_time_task_id" name="log_time[task_id]"></select> 
</div> 

和jQuery的邏輯

function populate_tasks(project_dp) { 

    div_node = project_dp.parent(); 

    next_div = project_dp.parent().next("div").find("select.task"); 
    alert(next_div.html()); // giving nothing. 
} 

,但我無法得到element(select.task)存在於next_div。當我用alert聲明看html時,那麼它什麼都沒有返回。

回答

1

其預期結果。,.html()獲取匹配元素的內容。

你得到什麼因爲select.task元素是因爲它沒有子元素。

根據評論,如果你想填充它。使用以下內容

next_div.append($('<option />', {text: 'SomeText', value:'SomeValue'})) 
+0

好的,我想要去獲得這個元素,我想用一些選項填充這個空的下拉菜單。我如何使用jQuery來做到這一點?我想要的一些示例代碼。 – John

+1

@John,更新了答案 – Satpal