我目前在http://roy-jin.appspot.com/jsp/textareaCounter.jsp使用此插件http://writer.knowconceptdesign.com/,希望创建一个开源的正确工具。一切正常,但是当Char限制为0或更小时,插件的字符串替换设置为不返回。如果char限制为0或更少,我想要做的是返回“None”或“No Limit set”。
以下是显示限制的插件代码。
function formatDisplayInfo(){
var format = options.displayFormat;
//When maxCharacters <= 0, #max, #left cannot be substituted.
if(maxCharacters > 0){
format = format.replace('#left', numLeft);
}
return format;
我试图在我的主页上覆盖这个
$(document).ready(function(){
var count_options = {
'maxCharacterSize': 0, // Just some default value
'originalStyle': 'originalDisplayInfo',
'warningStyle': 'warningDisplayInfo',
'warningNumber': 40,
'displayFormat': '#input Characters | #left Characters Left | #words Words'
};
// Initialise the plugin on document ready
$('#textinput').textareaCount(count_options);
$('#max_char').keyup(function () {
var max_char = +$(this).val();
//if the input is not a valid number
if ((isNaN($('#max_char').val())) || ($('#max_char').val() == 0)){
max_char = -1;
var char_left = count_options.displayFormat;
char_left = char_left.replace('#left', 'No Limit');
count_options.displayFormat = char_left;
}
count_options.maxCharacterSize = max_char;
// Unbind the 3 events the plugin uses before initializing it
$('#textinput')
.next('.charleft').remove().end()
.unbind('keyup').unbind('mouseover').unbind('paste')
.textareaCount(count_options);
});
这是我认为可以解决问题的块:
if ((isNaN($('#max_char').val())) || ($('#max_char').val() == 0)){
max_char = -1;
var char_left = count_options.displayFormat;
char_left = char_left.replace('#left', 'No Limit');
count_options.displayFormat = char_left;
无论我做什么,似乎都不会被覆盖。我认为即使是简单的查找和替换也行得通。我甚至尝试了一些查找和替换插件。
任何帮助都会很棒。
以下是可能想要查看的人的HTML:
<textarea cols="68" rows="21" name="textinput" id="textinput"></textarea><br/>
<input type="textbox" id="max_char" name="max_char" value="0" /> Max Characters <br/>
答案 0 :(得分:1)
假设我已经正确地理解了你(我不确定自己有没有),我猜你可以通过回调来解决这个问题 - 就像这样:
var options = {
'maxCharacterSize': 0,
'originalStyle': 'originalDisplayInfo',
'warningStyle': 'warningDisplayInfo',
'warningNumber': 40
};
var format = function(data) {
var left = data.left + " Characters Left",
maxChar = $('#max_char').val();
if (isNaN(maxChar) || maxChar <= 0) left = "No Limit";
return data.input + " Characters | " + left + " | " + data.words + " Words";
};
$('#textinput').textareaCount(options, format);
答案 1 :(得分:0)
我使用以下代码解决了这个问题:
$('#max_char').keyup(function () {
var max_char = +$(this).val();
var char_left = count_options.displayFormat;
if (isNaN(max_char) || max_char <= 0){
max_char= -1;
char_left = char_left.replace('#left', 'No Limit');
char_left = char_left.replace('Characters Left', '');
}
else{
char_left = char_left.replace('No Limit','#left Characters Left' );
}
count_options.maxCharacterSize = max_char;
count_options.displayFormat = char_left
更新这似乎更简单,不需要替换
if (isNaN(max_char) || max_char <= 0){
max_char= -1;
char_left='';
char_left='#input Characters | #words Words';
}
else{
char_left='';
char_left='#input of #max Characters | #left Characters Left | #words Words';
}