2017-08-04 1 views
1

私はレコードのテーブルを持っています。ユーザーがそれぞれ<td>をダブルクリックすると<input type="text">に変換され、その外側をクリックした後で保存する必要があります。 しかし、問題は特定の<td>の内部をクリックしたときです。でinput値を更新するAJAXコールせずにあなたの条件の小さな作業のデモでhttps://jsfiddle.net/gss6ezko/jqueryの外でクリックすると入力を保存する方法

+0

使用のonChange。 –

答えて

0

ここ:どのように

window.onclick = function(event) { 
    if (event.target != $('#tmp_ed')){ 
     //Your saving stuff 
    } 
} 

のようなものについてJSFiddle

//double click ->then convert to input 
 
$(document).on('dblclick', 'tbody tr td', function() { 
 
    var tmp = $(this).text(); 
 
    var name = $(this).attr("data-name"); 
 
    $(this).html('<input type="text" id="tmp_ed" value="' + tmp + '"/>'); 
 
    $(document).on('click', function() { 
 
    if ($(this).attr('id') !== "tmp_ed") { 
 
     var item = $('#tmp_ed').val(); 
 
     var tr = $('#tmp_ed').parent().parent(); 
 
     var id = tr.find('i').attr('id'); 
 
     var update = { 
 
     id: id, 
 
     request: "update", 
 
     val: item, 
 
     column: name 
 
     }; 
 
     $.ajax({ 
 
     url: "update.php", 
 
     method: "POST", 
 
     data: update, 
 
     success: function(e) { 
 
      alert(e); 
 
     } 
 
     }); 
 
     $(this).parent().html(item); 
 
     $(this).remove(); 
 
    } 
 

 

 
    }); 
 
});

1

あなたのデータベース:

0入力
+0

残念ながら、同じ問題に

//double click ->then convert to input var currentEle = ''; $(document).on('dblclick', 'tbody tr td', function(e) { e.stopPropagation(); currentEle = $(this); var tmp = $(this).text(); var name = $(this).attr("data-name"); $(this).html('<input type="text" id="tmp_ed" value="' + tmp + '"/>'); $("#tmp_ed").focus(); }); $(document).click(function(e) { if ($(e.target).closest('td').length == 0) $(currentEle).html($("#tmp_ed").val()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>test</td> </tr> </table>

korush

関連する問題