jQuery Word Counter,排除默认文本

时间:2013-08-30 14:36:34

标签: jquery

我的代码:

 function wordCount()
    {   
        if ($("#id_COMMENT_PRO).val() == "What did you like about the product?") {
            // do not subtract from count
        }

        if ($("#id_COMMENT_NEG).val() == "What didn't you like about the product?) {
            // do not subtract from count
        }

        var num = 100 - $("#id_COMMENT_PRO").val().length - $("#id_COMMENT_NEG").val().length;
        if (num < 0) {
            num = 0;
            }

        $('#count').text('Words left: ' + num);


    }

该函数从两个文本区域计数,但我需要它来排除默认文本,我开始编写IF语句,但我想不出一种方法来阻止它计数。

1 个答案:

答案 0 :(得分:0)

您只需稍微更改逻辑(假设用户输入内容后删除默认文本):

function wordCount() {
    var num = 100;

    if ($("#id_COMMENT_PRO").val() !== "What did you like about the product?") {
        num -= $("#id_COMMENT_PRO").val().length;
    }

    if ($("#id_COMMENT_NEG").val() !== "What didn't you like about the product?") {
        num -= $("#id_COMMENT_NEG").val().length;
    }

    num = num > 0 ? num : 0;

    $('#count').text('Words left: ' + num);
}