2016-08-05 14 views
0

に呼び出していない私はそうのような機能を持っている:jqueryの機能クリック

last_value = null; 
$('td').click(function(e) { 
    console.log($(e.target).attr("id")); 
    if ($(e.target).is("td")) { 
    var the_form = $("#edit-form").detach(); 
    if (last_value) { 
     $("#" + last_value.attr("id")).replaceWith(last_value); 
    } 

    last_value = $(e.target).clone(); 
    $(the_form).removeClass("hidden"); 
    var the_input = $(the_form).find("input.form-control"); 
    $(the_input).attr("name", $(e.target).attr("id")); 
    $(the_input).attr("placeholder", $(e.target).text()); 
    $(e.target).css('padding-top', 1); 
    $(e.target).css('padding-bottom', 1); 
    $(e.target).text(""); 
    $(e.target).append(the_form); 
    } 
}); 

これは、表のセルを取り、それが置き換えセルの内容が移入インラインフォームを生成することになっています。さらに、別のセルをクリックすると内容が元の値に戻るようにコードが追加されました。しかし、私が実行している問題は、これは、1つのセルをクリックすると仮定します。A.フォームは、必要な方法で表示されます。次に、セルBをクリックすると、フォームがそのセルに「移動」し、セルAの内容が元の値に戻ります。ここで再びセルAをクリックしたとします。この場合、フォームが表示されないだけでなく、セルBにも残ります。実際にはconsole.logは起動しません。私はここで間違って何をしていますか?

+0

あなたは何かエロスを受け取っていますか? – caramba

+0

静的なHTML上でclickイベントを実行している場合は、既にロードされていることを覚えておいてください(つまり、 DOM。その場合、$( 'td).click()を使うことができますが、DOMの読み込み後にhtmlスニペットを作成して追加した場合は$(' td).on( 'click'、function(){})を使用します。 –

+0

与えられた解決策が動作しない場合、あなたのコードにjsfiddleを用意することができます –

答えて

0

としては、問題を修正

$('table').on('click', 'td', function(e){ 
     console.log($(e.target).attr("id")); 
     if ($(e.target).is("td")) { 
      var the_form = $("#edit-form").detach(); 
      if (last_value) { 
       $("#" + last_value.attr("id")).replaceWith(last_value); 
      } 

      last_value = $(e.target).clone(); 
      $(the_form).removeClass("hidden"); 
      var the_input = $(the_form).find("input.form-control"); 
      $(the_input).attr("name", $(e.target).attr("id")); 
      $(the_input).attr("placeholder", $(e.target).text()); 
      $(e.target).css('padding-top', 1); 
      $(e.target).css('padding-bottom', 1); 
      $(e.target).text(""); 
      $(e.target).append(the_form); 
     } 

}); 
にコードを変更する、(おかげ@Mohammad Akbari)、OP下のコメントで示唆されました。

1

が、これはあなたを助けるこの

$(document).find('td').on('click',function(e){ 

      e.preventDefault(); 
      console.log($(e.target).attr("id")); 
      if ($(e.target).is("td")) { 
       var the_form = $("#edit-form").detach(); 
       if (last_value) { 
        $("#" + last_value.attr("id")).replaceWith(last_value); 
       } 

       last_value = $(e.target).clone(); 
       $(the_form).removeClass("hidden"); 
       var the_input = $(the_form).find("input.form-control"); 
       $(the_input).attr("name", $(e.target).attr("id")); 
       $(the_input).attr("placeholder", $(e.target).text()); 
       $(e.target).css('padding-top', 1); 
       $(e.target).css('padding-bottom', 1); 
       $(e.target).text(""); 
       $(e.target).append(the_form); 
      } 

    }); 

希望してみてください。

+0

'e.preventDefault()'は何をしますか? – Woody1193

+0

また、この解決策は私の問題を解決しません。 – Woody1193

+0

そして 'e.preventDefault()'は、実際には、 "Submit"ボタンが押されたときにフォームが送信されるのを防ぎます。 – Woody1193

関連する問題