2016-04-27 36 views
0

我正在嘗試實現Todo列表的內聯編輯。我有這個代碼,我希望能夠獲得它的價值。如何在jQuery中獲取.html()內的值

$(function clickedit() { 
    $(".a").dblclick(function (e) { 
     e.stopPropagation(); 

     var currentEle = $(this); 
     var value = $(this).html(); 
     var id_val = $(this).attr('value'); 
     //alert(id_val); 

     updateVal(currentEle, value, id_val);/**/ 
    }); 
}); 

function updateVal(currentEle, value, id_val) { 
    $(currentEle).html('<input class="thVal" id="aaa" type="text" value="' + value + '" />'); // i want to get the value inside the input 
    var aaa = $('#aaa').val(); 
    $(".thVal").focus(); 
    $(".thVal").keyup(function (event) { 



     if (event.keyCode == 13) { 
      alert(aaa); 

      $.post('includes/edit-task3.php', { task_name: aaa, task_id: id_val}, function() { 
       $(currentEle).html($(".thVal").val().trim()); 
       alert('in'); 
       //current_element.parent().fadeOut("fast", function() { $(this).remove(); }); 
      }); 
     } 
    }); 

    $(document).click(function() { 
      $(currentEle).html($(".thVal").val().trim()); 
    }); 
} 

如何獲取.html()中輸入的當前值?

我試過,var aaa = $('#aaa').val();但它不起作用..我該怎麼做?

非常感謝您的幫助。

+0

'$(currentEle).find( '#AAA')觸發的功能。VAL( );'試試這個 – guradio

+0

你做了這個被黑客入侵的版本'var aaa = value'; – madalinivascu

+0

@madalinivascu,或者只是使用'價值':P – Rayon

回答

1

不要把你的事件是由別的東西

$(".thVal").keyup(function (event) { 
     if (event.keyCode == 13) { 
      var aaa = $(this).val(); 
      alert(aaa); 

      $.post('includes/edit-task3.php', { task_name: aaa, task_id: id_val}, function() { 
       $(currentEle).html($(".thVal").val().trim()); 
       alert('in'); 
       //current_element.parent().fadeOut("fast", function() { $(this).remove(); }); 
      }); 
     } 
    }); 
0

使用.find(SELECTOR)

$(currentEle).find('#aaa').val(); 

編輯:作爲updateValfunction可以調用很多次,你將不得不在DOM具有相同value多個ID。確保ID必須是唯一的

+0

好的,非常感謝你的信息和你的幫助。不過,謝謝。 –