【问题标题】:copy first input field into the following input fields using jquery使用 jquery 将第一个输入字段复制到以下输入字段中
【发布时间】:2013-08-30 06:07:33
【问题描述】:

表格

<form id="form1" name="form1" method="post" action="">
  name_field
  <input type="text" name="field_1" id="field_1" />
  <a href="#" id="dup" name="dup" >Use same</a>
  <input type="text" name="field_2" id="field_2" />
  <input type="text" name="field_3" id="field_3" />
  ...
  <input type="text" name="field_x" id="field_x" />
</form>

我正在寻找一个将 field_1 克隆到 field_1 直到 field_x 的 ajax 脚本。 这是一种在所有玩家上填充团队名称的方式。

  $(".dup").click(function() {
    var myinput =  $( ".field_1" ).val();
    $("input[class^='field_']").html(myinput);
  });

$(".dup").live("click", function(e) {
    var count = $(".form1 fieldset").length + 1;
    $(this).parent().clone().insertBefore("#sbmt").find("input[type=text]").attr({
        "id": "input" + count,
        "name": "input" + count
    }).val("");
});

// 更新:

原来may字段是这种类型: 并且更改以 3 的倍数进行。所以

<input id="field_1" type="text" name="Item[fields][1]" ><a href="#" id="dup1">Use Info</a>
<input id="field_2" type="text" name="Item[fields][2]" ><a href="#" id="dup2">Use Team</a>
<input id="field_3" type="text" name="Item[fields][3]" ><a href="#" id="dup3">Use Year</a>
<input id="field_4" type="text" name="Item[fields][4]" >
<input id="field_5" type="text" name="Item[fields][5]" >
<input id="field_6" type="text" name="Item[fields][6]" >
<input id="field_7" type="text" name="Item[fields][7]" >
<input id="field_8" type="text" name="Item[fields][8]" >
<input id="field_9" type="text" name="Item[fields][9]" >
....
<input id="field_2" type="text" name="Item[fields][x]" >


so clicking 
dup1, clone field[1] into [4],[7] ...[1+3(i)]  
dup2, clone field[2] into [5],[8],...[2+3(i)] 
dup3, clone field[3] into [6],[9],...[3+3(i)]

我不能使用 id 值,因为它不一致。它有诸如 year_league66134 之类的值

【问题讨论】:

    标签: jquery forms input duplicates clone


    【解决方案1】:

    使用 .val() ,您没有定义 class 并使用它代替 class 使用 id 属性来解决您的问题。

    替换class

     $(".field_1").val();
     $("input[class^='field_']").html(myinput);
    

    ID

     $("#field_1").val();
     $("input[id^='field_']").val(myinput);
    

    【讨论】:

      【解决方案2】:

      您还需要使用 .val 来设置值

        $(".dup").click(function() {
          var myinput =  $( ".field_1" ).val();
          $("input[name^='field_']").val(myinput);
        });
      

      【讨论】:

        【解决方案3】:

        试试这个功能

        function CreateInputText(index) {
                var input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('id', 'field_' + index);
                input.setAttribute('name', 'field_' + index);
                $(input).insertBefore('#sbmt');
            }
        

        如果你需要的话,你可以在文件准备好的时候调用它

         $(document).ready(function () {
                for (var i = 0; i < 5; i++) {
                    CreateInputText(i);
                }
            });
        

        【讨论】:

        • 这个答案的问题是输入字段不限于5..它根据客户订购的T恤而变化
        • 使用一个变量,并根据客户订购的次数使用;)
        • 我实际上希望使用输入数组的长度:for ($i=1; i
        【解决方案4】:

        .val()

        改变

         $("input[class^='field_']").html(myinput);
        

         $("input[class^='field_']").val(myinput);
        

        【讨论】:

        • 这样的吗? for($i=1; i&lt;(Item[fields].length+1)/3; $i++) { $("input[name^='fields][1+3(i)").val(myinput); }
        猜你喜欢
        • 2012-07-24
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多