0

我有两个 javascript 函数,如下所示,它们将计算 textareas 中的非空行。

HTML:

<div id="map-devlist" >
    <div class="block-with-text-area">
    <div class="input-quest-with-text-area">Enter your device list(one device in one line)</div>
    <div class="input-resp-with-text-area"><span><textarea  class="textarea" id="devs" name="devs" type="text" onkeyup="devscount();"></textarea></span> </div> 
    </div>
</div>

<div id="lunidlist" >
    <div class="block-with-text-area">
        <div class="input-quest-with-text-area">Enter your LUN IDs in Hex(one ID in one line)</div>
        <div class="input-resp-with-text-area"><span><textarea  class="textarea" id="lunids" name="lunids" type="text" onkeyup="luncount();"></textarea></span> </div>  
    </div>
</div>

<div id="error1"></div>

Javascript:

function devscount()
{
    devcounts = 0;
    var lines = $("#devs").val().split("\n");
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].length > 0) devcounts++;
    }
}   

function luncount()
{
    luncounts = 0;
    var lines = $("#lunids").val().split("\n");
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].length > 0) luncounts++;
    }

    if(devcounts == luncounts) {
        var message ="both counts are matching";
    } else {
        var message ="Mismatch between values";
        document.getElementById('error1').innerHTML=message;
    }
}

现在我想比较这两个文本区域的行数,如果它们相等和不相等,我需要显示一条消息。但是上面的脚本对我不起作用。当我回 document.getElementById('error1').innerHTML=devscount;document.getElementById('error1').innerHTML=luncounts;它们相应的功能时,它完美地显示了行数。但我无法比较它们的值。怎么做 ?

JSFIDDLE 设置

4

2 回答 2

2

您的 jsfiddle 的问题不仅仅是问题。您尝试检查值的方式也存在问题。2 个文本区域正在运行函数来计算其中的项目,在 keyup 上,但没有运行比较代码。我将它们都更改为运行第三个函数,checkcounts并将脚本修改为此...

var devcounts;
var luncounts;

function devscount() {
    devcounts = 0;
    var lines = $("#devs").val().split("\n");
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].length > 0) devcounts++;
    }
}   

function luncount() {
    luncounts = 0;
    var lines = $("#lunids").val().split("\n");
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].length > 0) luncounts++;
    }
}

function checkcounts() {
    devscount();
    luncount();

    var message;

    if(devcounts == luncounts) {
        message ="both counts are matching";
    } else {
        message ="Mismatch between values";
    }

    document.getElementById('error1').innerHTML=message;
}

工作jsfiddle ...

于 2013-10-15T11:53:06.033 回答
2

令我震惊的是,您正在编写大量代码来做一些非常简单的事情。我已经在这个小提琴中替换了你的代码。单一功能

function testCounts()
{
 var devCounts = $('#devs').val().split('\n').length;
 var lunCounts = $('#lunids').val().split('\n').length;
 var equals = (devCounts == lunCounts)?'Equal':'Not Equal';
 $('#error1').text(equals);
}

还是我完全错了。

于 2013-10-15T12:04:55.530 回答