0

我有一行通过单击事件从一个也被克隆的表中克隆。我不确定这是否是我无法设置属性名称但只是为了清除它已被第二次克隆的原因。

var ipCount = 2;
$("#input_form").on("click", "#add_input_param", function() {
    $('#input_param tr').eq(1).clone().find('input').val('').end()
            .appendTo('#input_param > thead').trigger('creat')
            .find('*[name]')
            .each(function() {
       //alert(i);
       alert( $(this).attr('name'));
        $(this).setAttribute = ('name', 'new_name');
            });
    ipCount++;
});

现在我的问题是:1.为什么我可以获取属性但不能设置它。2. 我希望我的 .each(function(){}) 仅循环通过此点击事件创建的最后一行。(并非所有人都喜欢现在显示的内容)

任何帮助我都会appriciate它。先谢谢了。

这是我的 HTML 块:

    <h4>
                        3.3.5 Input Parameters
                    </h4>
                    <table id="input_param" >
                        <thead>
                            <tr>
                                <th>Parameter</th>
                                <th>Data Type</th>
                                <th>Required</th>
                                <th>Brief description</th>
                                <th>Location in Request</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><input type="text" name=":ops1/6/1/1_param" /></td>
                                <td><input type="text" name=":ops1/6/1/1_type" /></td>
                                <td>
                                    <select id="required" name=":ops1/6/1/1_required">
                                        <option value="Mandatory" >Mandatory</option>
                                        <option value="Optional" >Optional</option>
                                        <option value="Conditional" >Conditional</option>
                                    </select>
                                </td>
                                <td><textarea name=":ops1/6/1/1_desc"></textarea></td>
                                <td>
                                    <select name=":ops1/6/1/1_location">
                                        <option value="Header" >Header</option>
                                        <option value="Body" >Body</option>
                                        <option value="Query_param" >Query Parameter</option>
                                        <option value="Resource_uri" >Resource URI</option>
                                    </select>
                                </td>
                            </tr>
                        </tbody>
                    </table>     
                    <input type="button" id="add_input_param" value="+ Add Input Parameter" data-inline="true" /><br />
4

1 回答 1

2

当前代码格式不正确。

这个

 $('#input_param tr').eq(1).clone().find('input').val('').end()
        .appendTo('#input_param > thead').trigger('creat')
        .find('*[name]')
        .each(function() {
   //This is working.
   alert( $(this).attr('name'));
   //This is not working.
   $(this).attr("name", "new_name"
        });

应该

 $('#input_param tr').eq(1).clone().find('input').val('').end()
        .appendTo('#input_param > thead').trigger('creat')
        .find('*[name]')
        .each(function() {
   //This is working.
   alert( $(this).attr('name'));
   //This is not working.
   $(this).attr("name", "new_name"); //<-- added );
        });
于 2013-09-17T19:18:00.433 回答