2016-08-05 2 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); 
    } 
}); 

. 또한 다른 셀을 클릭하면 내용이 원래 값으로 되돌아 갈 수 있도록 코드가 추가되었습니다. 그러나, 내가 겪고있는 문제는 이것입니다 : 하나의 셀을 클릭하면, A. 폼이 있어야하는 것처럼 보입니다. 그런 다음 셀 B를 클릭하면 양식이 해당 셀로 "이동"하고 셀 A의 내용이 원래 값으로 되돌아갑니다. 이제 셀 A를 다시 클릭한다고 가정합니다. 이 경우 폼이 나타나지 않을뿐만 아니라 셀 B에도 그대로 유지됩니다. 실제로 console.log은 실행되지 않습니다. 여기서 내가 뭘 잘못하고 있니?

+0

에로가 있습니까? – caramba

+0

정적 HTML에서 click 이벤트를 수행하는 경우, 이미로드되어있는 것을 의미합니다 (예 : click, function() {/ * your code * /}); DOM. 이 경우 $ ('td') .click()을 사용할 수 있지만 DOM을로드 한 후 HTML 스 니펫을 만들고 추가 한 경우 $ ('td) .on ('click ', function() {})을 사용해야합니다. –

+0

주어진 솔루션이 작동하지 않으면 코드에 jsfiddle을 준비 할 수 있습니까? –

답변

0

으로는 영업 이익에서 코멘트에 제안했다 (감사 @Mohammad Akbari),

$('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); 
     } 

}); 

수정 문제에 코드를 변경.

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