[英]How to update an appended value?
伙計們,我有輸入文字,當我開始輸入文字時,它會計算出我正在使用的字母數。 但是當我清除輸入值時,無法更改字母計數器。 我怎么才能得到它? 提前致謝。 的jsfiddle
function count_letter() { var len = $("#area").val().length; $('#counter').append(len); } $('#area').bind('keyup', function() { $('#counter').html(''); count_letter(); }); $('#clear-value').click(function(){ $("#area").val(" "); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="area"><br> <div id="counter"></div><br> <button id="clear-value"> Clear value </button>
在#clear-value
事件處理程序中,添加:
$('#counter').empty();
這將清除#counter
元素。
function count_letter() { var len = $("#area").val().length; $('#counter').append(len); } $(function() { $('#area').bind('keyup', function() { $('#counter').html(''); count_letter(); }); $('#clear-value').click(function(){ $("#area").val(""); $('#counter').empty(); //<<<----- }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" id="area"><br> <div id="counter"></div><br> <button id="clear-value"> Clear value </button>
清除輸入值時只需調用count_letter函數:
$('#clear-value').click(function(){
$("#area").val(" ");
$('#counter').html('');
count_letter();
});
單擊清除值按鈕清除計數器。 參見以下內容: https : //jsfiddle.net/28bs18gq/2/
function count_letter() {
var len = $("#area").val().length;
$('#counter').append(len);
}
$('#area').bind('keyup', function() {
$('#counter').html('');
count_letter();
});
$('#clear-value').click(function(){
$("#area").val(" ");
$('#counter').html('');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.