2011-08-23 42 views
0
if (fromObject == null) fromObject = primaryTable; 
    var id = fromObject+"_"+toObject; 
    var tr = $('<tr id="'+id+'"></tr>').appendTo($("#joins")); 

    tr.append('<td width=20%><select style="width:100%" type="text" name="from_object" id="from_object" value=""></select></td>'); 
    tr.append('<td width=20%><select style="width:100%" type="text" name="to_object" id="to_object" value=""></select></td>'); 
    tr.append('<td width=20%><input style="width:100%" type="text" name="from_column" id="from_column" value="" readonly></td>'); 
    tr.append('<td width=20%><input style="width:100%" type="text" name="to_column" id="to_column" value="" readonly></td>'); 
    tr.append('<td width=20%><select style="width:100%" type="text" name="join_type" id="join_type" value=""></select></td>'); 


    var combo3 = $("select[name=from_object]"); 

這是我的問題。我經歷了多次追加的過程,並留下了許多具有相同名稱的選擇標籤。我想弄清楚如何選擇'from_object'。我必須動態地執行此操作嗎?現在combo3總是選擇第一個標籤。從同一名稱的多個標籤中選擇<select>

+1

名稱看起來不一樣。 – ShankarSangoli

+0

你的問題是什麼?如果你想選擇一個名字爲'from_object'的選擇,那麼你已經釘了它。它總是選擇第一個的原因是因爲它總是第一個?根據您的反饋, –

回答

1

我不完全理解這個問題。爲了獲得第一次使用

$('select[name=from_object]:first').doWhatever(): 

獲得所有使用

$('select[name=from_object]').each(function(){ 
    $(this).doWhatever(); 
}); 

但是從來沒有使用相同的ID兩次。

0

您的html無效與這些重複的ID。我試圖同時解決你所要求的和重複的ID問題。它應該給你一個解決方案的良好開端,如果不是直接開箱即用。

編輯答案

if (fromObject == null) fromObject = primaryTable; 
    var id = fromObject+"_"+toObject; 
    // don't append to the DOM until the last second, i moved tr.appendTo() to the end 
    var uniqueIdIndex = $("tr[id^='"+id+"_tr'").length +1; // get a unique index 
// the idea of cloning HTML and appending it takes some getting used to, but makes it easy to change and maintain. don't let it throw you 
// all this cloning stuff is to get you some Unique Ids. 
     var masterTR = $('<tr id="'+id+'_tr_'+uniqueIdIndex+'"></tr>'); 
     var masterTD = $('<td width="20%"></td>'); 
     var masterDD = $('<select type="text" style="width:100%"></select>'); 
     var masterCI = $('<input style="width:100%" type="text" value="" readonly></input>'); 
     // grab a default drop down, set the name, ID and value 
     var currentInput = masterDD.clone().attr({"name":"from_object","id": id+"_from_object_"+uniqueIdIndex,"value":""}); 
     var tr = masterTR.clone(); // get a Row 
     tr.append(masterTD.clone().append(currentInput)); // clone a td, append the input, and append that to the row 
     // grab a default drop down, set the name, ID and value 
     currentInput = masterDD.clone().attr({"name":"to_object","id": id+"_to_object_"+uniqueIdIndex,"value":""}); 
     tr.append(masterTD.clone().append(currentInput)); 
     currentInput = masterCI.clone().attr({"name":"from_column","id": id+"_from_column_"+uniqueIdIndex,"value":""}); 
     tr.append(masterTD.clone().append(currentInput)); 
     currentInput = masterCI.clone().attr({"name":"to_column","id": id+"_to_column_"+uniqueIdIndex,"value":""}); 
     tr.append(masterTD.clone().append(currentInput)); 
     currentInput = masterDD.clone().attr({"name":"join_type","id": id+"_join_type_"+uniqueIdIndex,"value":""}); 
     tr.append(masterTD.clone().append(currentInput)); 

      tr.appendTo($("#joins")); 
//optionally, find the most recently added from_column "select[name='from_column']" 
      var combo3 = $("[id^='"+id+"_from_column]", "#joins").filter(":last"); 

舊答案

如果(fromObject == NULL)fromObject = primaryTable; var id = fromObject +「_」+ toObject; var tr = $('')。appendTo($(「#joins」));

tr.append('<td width=20%><select style="width:100%" type="text" name="from_object" id="'+id+'from_object" value=""></select></td>'); 
    tr.append('<td width=20%><select style="width:100%" type="text" name="to_object" id="'+id+'to_object" value=""></select></td>'); 
    tr.append('<td width=20%><input style="width:100%" type="text" name="from_column" id="'+id+'from_column" value="" readonly></td>'); 
    tr.append('<td width=20%><input style="width:100%" type="text" name="to_column" id="'+id+'to_column" value="" readonly></td>'); 
    tr.append('<td width=20%><select style="width:100%" type="text" name="join_type" id="'+id+'join_type" value=""></select></td>'); 



    var combo3 = $("#"+id+"from_object"); 
+0

更改了我的答案 – DefyGravity

相關問題