0

我的代码:

 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 语句,但我想不出一种方法来阻止它计数。

4

1 回答 1

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);
}
于 2013-08-30T14:54:57.277 回答