自定义JQuery克隆行属性 [英] Customizing JQuery Cloned row attributes

查看:103
本文介绍了自定义JQuery克隆行属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究动态添加表行的JQuery方法.一个出色的线程是:

I have been researching the JQuery way to add table rows dynamically. One excellent thread is: How to Copy Table Row with clone in jquery and create new Unique Ids for the controls, with the last example being the one I am targeting in this post.

我有一个小提琴,提供了我要做的事的示例.这个小提琴还不能完全正常工作,但是我正在努力,

I have a fiddle giving an example of what I am trying to do. This fiddle does not work exactly yet, but I am working on it,

我遇到的主要问题是获取表行副本以设置不同类型的列元素id和默认值,甚至行属性.从本质上讲,我该如何扩展它以使其更健壮.

The main issue I am having is getting the table row copy to set different types of column elements id and default values, and even row attributes. In essence, how do I extend this to be more robust.

感谢Nick Craver,我正在尝试使用此功能:

Thanks to Nick Craver, I am trying to use this:

// do Add row options
$("#Add").click(function() {

    var rowCount = $('#secondaryEmails >tbody >tr').length;
    var i = rowCount + 1;
    alert('rowCount: ' + rowCount + ', new row: ' + i);
    $("#secondaryEmails >tbody tr:first").clone().find("input").each(function() {
        $(this).attr({
            'id': function(_, id) {
                return id + i
            },
           'name': function(_, name) {
                return name + i
            },
            'value': ''
        });
    }).end().appendTo("#secondaryEmails >tbody");
});

这将很好地复制并插入一行,但是如果我有一个带有单选按钮,输入框和选择列表的行,则我不知道如何告诉它根据类型设置每个元素的默认值的元素.我正在尝试使用模板行,但这意味着我需要在从nonetable-cell的行上设置style:display属性.再次,如何?

which will copy and insert a row nicely, but if I have a row with a radio button, input box, and select list, I cannot figure out how to tell it to set the default value of each element depending on the type of element. I am trying to use a template row, but that means I need to set the style:display attribute on the row from none to table-cell. Again, how?

有关工作示例,请参见前面提到的小提琴.

Please see the fiddle mentioned previously for a working example.

推荐答案

这现在显示该行: http://jsfiddle.net/EwQUW/5/

您可能希望在元素上使用.show()进行显示,从而将样式有效地设置为display:block而不是display:none;

You would want to use .show() on the element to show it, which effectively sets the style to display:block instead of display:none;

这篇关于自定义JQuery克隆行属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆