2016-06-15 5 views
-1

私はこのコードを持っている:(私はここのオフ盗んだ)クリック/クローンでjQueryでHTML値をインクリメントする?

<script> 

var regex = /^(.+?)(\d+)$/i; 
var cloneIndex = $(".clonedInput").length; 

function clone(){ 
    $(this).parents(".clonedInput").clone() 
     .insertAfter(".clonedInput:last") 
     .attr("id", "clonedInput" + cloneIndex) 
     .find("*") 
     .each(function() { 
      var id = this.id || ""; 
      var match = id.match(regex) || []; 
      if (match.length == 3) { 
       this.id = match[1] + (cloneIndex); 
      } 
     }) 
     .on('click', 'span.clone', clone) 
     .on('click', 'span.remove', remove); 
    cloneIndex++; 
} 
function remove(){ 
    $(this).parents(".clonedInput").remove(); 
} 
$("span.clone").on("click", clone); 

$("span.remove").on("click", remove); 

</script> 

は今、それがうまく複製し

<div id="clonedInput1" class="clonedInput"> 
       <div class="row" id="item1"> <!-- item --> 

       <div class="col-md-6"> 
        <!-- // --> 
        <label for="one">external_ref</label> 
        <input type="text" id="external_ref" name="item[0][external_ref]" class="form-control" value="<?php echo rand(1,9999); ?><?php echo strtr($date, array('-' => '', ' ' => '',':' =>'')); ?>"> 
        <!-- // --> 
        <label for="one">style</label> 
        <input type="text" name="item[0][style]" class="form-control" value="mens"> 
        <!-- // --> 
        <label for="one">size</label> 
        <input type="text" name="item[0][size]" class="form-control" value="large"> 
        <!-- // --> 
        <label for="one">color</label> 
        <input type="text" name="item[0][color]" class="form-control" value="white"> 
        <!-- // --> 
        <label for="one">print_location</label> 
        <input type="text" name="item[0][print_location]" class="form-control" value="front"> 
       </div> 

       <div class="col-md-6"> 
        <!-- // --> 
        <label for="one">quantity</label> 
        <input type="text" name="item[0][quantity]" class="form-control" value="2"> 
        <!-- // --> 
        <label for="one">x_offset</label> 
        <input type="text" name="item[0][print_x_offset]" class="form-control" value="10"> 
        <!-- // --> 
        <label for="one">y_offset</label> 
        <input type="text" name="item[0][print_y_offset]" class="form-control" value="10"> 
        <!-- // --> 
        <label for="one">external_url</label> 
        <input type="text" name="item[0][external_url]" class="form-control" value="https://upload.wikimedia.org/wikipedia/commons/thumb/9/96/Sass_Logo_Color.svg/1280px-Sass_Logo_Color.svg.png"> 
        <!-- // --> 
        <label for="one">external_thumbnail_url</label> 
        <input type="text" name="item[0][external_thumbnail_url]" class="form-control" value="https://upload.wikimedia.org/wikipedia/commons/thumb/9/96/Sass_Logo_Color.svg/1280px-Sass_Logo_Color.svg.png"> 
       </div><!-- end right --> 
       </div><!-- end item --> 
       <div class="actions"> 
        <span class="clone btn btn-default">Clone</span> 
        <span class="remove btn btn-danger">Remove</span> 
       </div> 
      </div> 

そしてHERESに私のjQueryを、私はそのビットを行うことができます。

しかし、私は何を必要とする、私は[1] [0]の項目に項目から「名前」タグのすべてを変更する必要がdiv要素が

クローン化されているすべての時間のためにある...アイテム[2]とそういうわけで、各クローンには独自のアイテム番号があります。

これはjSONをファイルメーカーのapiに送信できるようにするためです。

+0

代わりにどのIDが得られますか? –

+0

インデックスに基づいてjsonを作成するときに後で生成することはできませんか? –

答えて

0
var myClones = $(".clonedInput"); 

for (var n = 0; n < myClones.length; n++) { 
    myClones[n].setAttribute('name', 'clonedInput'+n); 
} 
+0

これはうまく動作しませんでした。 ここにはhttps://jsfiddle.net/5w0uwsxd/ – Blackline

+0

があるiveのフィードバックがあります。また、フィドルでは、クローンのコメントブロックがあり、クローン内のアイテム[0]をアイテム[1]に変更し、それは次のクローンのectの項目[2]に変更されるでしょう。 – Blackline

関連する問題