简体   繁体   English

jQuery更改在console.log中工作的.each()中name属性的值,但不更改页面源代码

[英]jQuery change the value of the name attribute in .each() working in console.log but not page source

I am trying to change the name attribute of a cloned table row. 我正在尝试更改克隆表行的名称属性。 Here is my code: 这是我的代码:

    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')
            .find('*[name]')
            .each(function() {
        console.log($(this).attr('name'));
        $(this).attr('name', 'new_name');
        console.log($(this).attr('name'));
    });
    ipCount++;
});

on the console it changed the name attr to new_name but not showing when I do Inspect Element on Chrome DevTools. 在控制台上,它将名称attr更改为new_name,但在Chrome DevTools上执行元素检查时未显示。 This is the console: 这是控制台:

    :ops1/6/1/1_param js.js:48
    new_name js.js:50
    :ops1/6/1/1_type js.js:48
    new_name js.js:50
    :ops1/6/1/1_required js.js:48
    new_name js.js:50
    :ops1/6/1/1_desc js.js:48
    new_name js.js:50
    :ops1/6/1/1_location js.js:48
    new_name 

HTML after click shows on DevTools: 单击后的HTML在DevTools上显示:

    <input type="text" name=":ops1/6/1/1_param" class="ui-input-text ui-body-c">

I been trying to interate the name attribute but it doesn't seem to change when I do it. 我试图插入name属性,但是当我这样做时似乎并没有改变。

My HTML table( the row was cloned from a cloned table) it might be the reason why it's not changing the name attr) 我的HTML表(该行是从克隆表中克隆的),这可能是它没有更改名称attr的原因

    <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 />
$('table tr').appendTo('#foo').find('.bar')

doesn't find elements in '#foo' element, it will find elements that has classed '.bar' inside of 'table tr'. 不会在'#foo'元素中找到元素,它将在'table tr'中找到已归类为'.bar'的元素。 So, your chain is not that you want. 因此,您的连锁不是您想要的。 You can write like that: 您可以这样写:

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')
            .find('#input_param > thead [name]')
            .each(function() {
                $(this).attr('name', 'new_name');
            });
    ipCount++;
});
<input type="text" name=":ops1/6/1/1_param" class="ui-input-text ui-body-c">

将是.eq(0) ,以及@MuratÇorlu所说的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM