我正在研究限制標記,因此max將爲5,但對於此測試,我將使用2,但是在行尾有一個額外的逗號,當嘗試添加另一個逗號時標籤超過最大允許值。我如何刪除逗號?刪除行尾的額外逗號
<body>
<input id="input" type="text" />
</body>
$("#input").keypress(function(e){
var value = $(this).val().replace(" ", "");
var words = value.split(",");
if(words.length > 2){
//alert("Hey! That's more than 5 words!");
e.preventDefault();
}
});
DEMO:http://jsfiddle.net/BzN5W/2/
[RTRIM在javascript]的可能的複製(http://stackoverflow.com/questions/8791394/rtrim-in-javascript) – Rasclatt