Jquery 从具有相同名称的多个标记中选择
这是我的问题。我经历了多次追加的过程,留下了许多同名的select标记。我想知道如何选择“from_对象”。我必须动态地这样做吗?现在combo3总是选择第一个标记 我不完全理解这个问题。第一次使用Jquery 从具有相同名称的多个标记中选择,jquery,select,append,Jquery,Select,Append,这是我的问题。我经历了多次追加的过程,留下了许多同名的select标记。我想知道如何选择“from_对象”。我必须动态地这样做吗?现在combo3总是选择第一个标记 我不完全理解这个问题。第一次使用 if (fromObject == null) fromObject = primaryTable; var id = fromObject+"_"+toObject; var tr = $('<tr id="'+id+'"></tr>').appen
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]");
获得全部使用权
$('select[name=from_object]:first').doWhatever():
但是,切勿使用同一ID两次。您的html对于这些重复ID无效。我试图同时解决你的要求和重复身份问题。它应该给你一个很好的解决方案的开始,如果不是直接开箱即用的话 编辑答案 旧答案 如果fromObject==null,则fromObject=primaryTable; 变量id=fromObject+\uU9+toObject; var tr=$.appendTo$连接
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");
你的问题是什么?如果要选择一个具有来自\u对象的名称的select,则已锁定该对象。它总是选择第一个的原因是因为它总是第一个?根据您的反馈更改了我的答案
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");
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");