【问题标题】:How do I change the input name in jQuery如何更改 jQuery 中的输入名称
【发布时间】:2016-01-05 22:10:29
【问题描述】:

这是我目前正在努力实现的目标:

我正在尝试克隆表单并更改输入名称。我将如何做到这一点。

JS:

$(document).ready(function() {
var counter = 0;
var MAX_FIELDS = 3;
var MIN_FIELDS = 1;
var form = $('.force-group');

$('#add').on('click', function(e) {
    if (counter < MAX_FIELDS) {
        //$(".form-group").clone().appendTo("#forceForm");
        $.each(form, function(i) {
            var clone = $('.force-group').first().clone();
            clone.children().val("");

            $(this).parent().append(clone);
            if (counter == 0) {
                $("b:eq(1)").html("<b> Force 2</b>");
            };
            if (counter == 1) {
                $("b:eq(3)").html("<b> Force 3</b>");
            };
            if (counter == 2) {
                $("b:eq(5)").html("<b> Force 4</b>");
            };

        });
        counter++;
    };
});

html:

  <#import "template.ftl" as layout />
  <@layout.template title="Force" js="/js/force.js" css="/css/force.css">

<h3 class = "text-center">Total Force</h3>
<hr>

<div class="col-md-6 col-md-offset-3 col-xs-8 col-xs-offset-2">
    <div class="force-selector input_fields_wrap">
        <a id = 'add' href="#" class="btn btn-primary col-md-5">Add Force</a>
        <a id = 'remove' href="#" class="btn btn-warning col-md-5 col-md-offset-2">Remove Force</a>
    </div>
</div>
<div class="col-md-6 col-md-offset-">
    <div class="col-xs-12">
        <div class = "well" id="force-input">
            <form id = "forceForm" class = "form-horizontal" method = "POST" autocomplete="off">
                <fieldset>
                    <h4 class="text-center" id="form-title"><strong>Input</strong></h4>
                    <div class="form-group force-group">
                        <label class = "control-label col-md-2"><b>Force 1</b></label>
                        <label class = "control-label col-md-2">Magnitude</label>
                        <div class = "col-md-3 force-info">
                            <input type = "text" class="form-control" name="0force" autocomplete="off">
                        </div>
                        <label class = "control-label col-md-2">Angle</label>
                        <div class = "col-md-3 force-info">
                            <input type = "text" class="form-control" name="0angle" autocomplete="off">
                        </div>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
</div>
<div class = "col-md-5">
    <table id = "forceChart" class="table table-striped table-hover">
        <thead>
        <tr class="table">
            <th class="head-pad">Total Force</th>
        </tr>
        </thead>
        <tbody>
        <tr class="table">
            <td class="pad">Net Force:</td>
        </tr>
        </tbody>
        </thead>
        <tbody>
        <tr class="table">
            <td class="pad">Direction:</td>
        </tr>
        </tbody>
        <tfoot></tfoot>
    </table>
</div>
<div class = "col-md-4 col-md-offset-4 col-xs-6 col-xs-offset-3">
    <div class="form-group">
        <div class = "col-md-6 col-xs-offset-3">
            <input id = "calculate" class = "btn btn-success btn-block" type="submit" value="Calculate!">
        </div>
    </div>
</div>

这是这里的 jsfiddle 链接:

Click here for the link.

PS:我正在使用模板,所以 jsfiddle 看起来有点糟糕。

我是 jQuery 新手,我一直在寻找解决方案,但我似乎陷入了困境。

我尝试将 .last() 与 attr 编辑一起使用,但这似乎没有任何作用。

【问题讨论】:

  • 把名字改成什么?

标签: javascript jquery html


【解决方案1】:

要更改输入名称,只需使用 attr() 方法

$('input').attr('name', 'yourNewname');

我说您输入的内容没有 ID,因此您应该使用以下方法之一:

1/ 给每个输入一个 ID,例如 #input1 #input2

$('#input1').attr('name', 'yourNewname1'); // change the name of first input

$('#input2').attr('name', 'yourNewname2'); // change the name of first input

你可以使用 eq()

$('input').eq(0).attr('name', 'yourNewname1'); // change the name of first input

$('input').eq(1).attr('name', 'yourNewname2'); // change the name of first input

【讨论】:

    【解决方案2】:

    要更改包括名称在内的任何属性,您可以这样做

    $('#myInputFieldId').attr('name','new_name')
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      试试这个:$('input[name="0angle"]').attr('name', 'new-name');

      【讨论】:

        猜你喜欢
        • 2020-05-01
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-06
        • 1970-01-01
        • 2020-01-25
        • 2017-06-12
        相关资源
        最近更新 更多