2016-06-23 8 views
0

複製された要素の属性を変更しようとしていますが、動作していないようです。複製された要素の属性を変更する

私のjqueryのコード:

$.ajax({ 
     url: _url, 
     type: "POST", 
     data: { note: _note, owner_id: _owner_id, registration_number: _registration_number }, 
     dataType: "json" 
    }).done(function(data){ 
     var tunnus = data.id; 
     if(editNoteId == null){ 
      var firstTableRow = $(".note-table").find("tr").first().clone(true); 

      var editLink = $(".edit-note").first().clone(true).data("note-id", id); 


      firstTableRow.children("td")[0].removeAttribute("class"); 
      firstTableRow.children("td")[0].setAttribute("class", tunnus); 
      firstTableRow.children("td")[0].innerHTML = data.info; 
      firstTableRow.children("td")[1].innerHTML = "timestamp"; 
      firstTableRow.children("td")[2].innerHTML = data.laatija; 
      firstTableRow.children("td")[3] = editLink; 

      $(".note-table").prepend(firstTableRow); 



     } 

マイ要素:

<table class="table table--list note-table"> 
            <tbody><tr> 
        <td class="8038">now</td> 
        <td>timestamp</td> 
        <td>company</td> 
        <td> <a href="#" class="edit-note" data-toggle="modal" data-target="#notesModal" data-note-id="8037">Muokkaa</a></td> 
       </tr><tr> 
        <td class="8037">fug</td> 
        <td>23.6.2016 12:51:29</td> 
        <td>company</td> 
        <td> <a href="#" class="edit-note" data-toggle="modal" data-target="#notesModal" data-note-id="8037">Muokkaa</a></td> 
       </tr> 
            <tr> 
        <td class="8036">another</td> 
        <td>23.6.2016 12:45:27</td> 
        <td>avoltus</td> 
        <td> <a href="#" class="edit-note" data-toggle="modal" data-target="#notesModal" data-note-id="8036">Muokkaa</a></td> 
       </tr> 
</table> 

editLinkはまだそれがからクローン化された要素と同じデータ・ノート-IDを持っていますが、まだ何に変わりますそれは仮定されている

+0

に値です設定しているものである

firstTableRow.find("td:nth-child(4)").data("note-id", editLink); 

を...? – wmash

+0

また、 'data.info'はどこにも定義されていないので、' data.info'は未定義またはエラーを返します – wmash

+0

私の投稿を更新しました。両方ともajaxリクエストです – user3482304

答えて

0

ここでは、あなたが望むもののようなもののjsFiddle何かの(注:私は変数のうちのnstead(すなわち、トンネル)私はそれらにアクセスする必要はありません

注意すべきいくつかのこと:

.children

これは、あなたがでそれを指している要素のALL子を取得します。このメソッドはで、は特定の子要素を探します。私が混ざってしまった

:これを行うには、.find()または.closest()

補正を使用しています。

firstTableRow.children("td")[0].removeClass(); 
:たとえば

( - あなたは(正しいものを得るために、角括弧[??] <を使用)*あなたがそれをやっていた方法をchildren()で特定の子を選択しなくすることができます

...なる...

firstTableRow.children("td:nth-child(1)").removeClass(); 

...か...

firstTableRow.children("td:first-child").removeClass(); 

.setAttribute()/.removeAttribute()

これらは便利な方法ですが、あなたがやっていることのために、それは.removeClass().addClass()

.first()

を使用するように、より良く、より読みやすいアイムこの方法に精通しているわけではなく、ちょうど:nth-child()を使用することが、あなたが望む子供を得るためのより広く使用される方法であることに注意すること。 。 は、(取得するかを決定するために括弧内の整数を通過(すなわち1、2、3))また

、もし要素editLinkになるように設定最後の行?物事の見た目では、実際には、要素全体ではなく、その要素の属性をdata-note-idに設定する必要があります。あなたは使用することができます:最初の引数はdata-*あなたが選択している属性、そして第二に、あなたはそれが変数 `tunnus`は何

関連する問題