选择<选择>来自具有相同名称的多个标签

发布于 2024-12-01 03:47:08 字数 1093 浏览 1 评论 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 始终选择第一个标签。

    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]");

Here is my issue. I go through this process of appending multiple times and am left with many select tags with the same name. I would like to figure out how to select the 'from_object'. Do I have to do this dynamically? Right now combo3 always selects the first tag.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-12-08 03:47:08

我不完全理解这个问题。获得第一次使用

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

获得所有使用

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

但是切勿两次使用相同的 ID。

I don't fully understand the question. To obtain the first use

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

To obtain all use

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

However never use the same ID twice.

似最初 2024-12-08 03:47:08

您的 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");

旧答案

if (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");

your html is not valid with those duplicate IDs. I tried to solve both what you asked for and the duplicate ID problem at once. it should give you a good start on a solution, if not work straight out of the box.

edited answer

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");

old answer

if (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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文