【问题标题】:After calling Jquery Val(), Append() is not working调用 Jquery Val() 后,Append() 不起作用
【发布时间】:2017-12-29 23:48:33
【问题描述】:

我是 HTML、JavaScript 和 Jquery 的新手。在我调用 Val("") 之后,我似乎无法弄清楚为什么 Append() 不再将文本附加到 TextArea。我需要调用 Val("") 的原因是我想在每次开始搜索时清除 TextArea 中的文本。所以在 UI 上,我会输入游戏或公司名称的文本,然后按下按钮根据输入字段搜索游戏。下面是我使用的 site.js 中的代码。谢谢。

 var arr = [
    {
        Game:"Double Dragon",
        Developer: "Technos Japan Corp",
        Publisher: "Acclaim",
        Platform: {
            Console: "Nintendo"
        }
    },{
        Game:"Street Fighter 2000",
        Developer: "Capcom",
        Publisher: "Capcom",
        Platform: {
            Console: "Nintendo"
        }   
    },{
        Game:"Super Mario Bros.",
        Developer: "Nintendo",
        Publisher: "Nintendo",
        Platform: {
            Console: "Nintendo"
        }   
    },{
        Game:"Secret Mana",
        Developer: "SquareSoft",
        Publisher: "SquareSoft",
        Platform: {
            Console: "Super Nintendo"
        }   
    },{
        Game:"Final Fight",
        Developer: "Capcom",
        Publisher: "Capcom",
        Platform: {
            Console: "Super Nintendo"
        }   
    },{
        Game:"Super Contra",
        Developer: "Konami",
        Publisher: "Konami",
        Platform: {
            Console: "Nintendo"
        }   
    },{
        Game:"Mega Man",
        Developer: "Capcom",
        Publisher: "Capcom",
        Platform: {
            Console: "Nintendo"
        }   
    }
];


function GameBtnEvent()
{
    //$("#textAreaText").val('');//if I comment this out, Append() call will work, otherwise Append() does not append text to TextArea
    DisplayResults();
}

function DisplayResults()
{
    var found = 0;

    $.each(arr, function (index, value) {
        var gameName = $("#searchTitle").val();
        var companyName = $("#selectionBlock").val();

        if(companyName.toLowerCase() == value.Publisher.toLowerCase())
        {
            $('#textAreaText').append("Title: " + value.Game + "\n");
            $('#textAreaText').append("Company: " + value.Publisher + "\n");
            $('#textAreaText').append("Console: " + value.Platform.Console + "\n\n");
            found = 1;
        }
        else if(companyName.toLowerCase() == value.Publisher.toLowerCase() &&
                 gameName.toLowerCase() == value.game.toLowerCase() )
        {
            $('#textAreaText').append("Title: " + value.Game + "\n");   
            $('#textAreaText').append("Company: " + value.Publisher + "\n");
            $('#textAreaText').append("Console: " + value.Platform.Console + "\n\n");
            found = 1;
        }
    });

    if(found == 0)
    {
        $("#textAreaText").append("game not found");
    }
}

更新: 貌似这种行为只发生在 Chrome 上,但是 Explorer 没有问题。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    假设#textAreaText 是一个<textarea> 元素,那么您首先不应该使用append()。在所有情况下都使用val()。如果您需要附加到现有值,请向val() 提供一个函数,该函数接受当前值作为返回新值时可以使用的参数,如下所示:

    function GameBtnEvent() {
      $("#textAreaText").val('');
      DisplayResults();
    }
    
    function DisplayResults() {
      $.each(arr, function(index, value) {
        if ($("#selectionBlock").val().toLowerCase() == value.Publisher.toLowerCase() || $("#searchTitle").val().toLowerCase() == value.game.toLowerCase()) {
          $('#textAreaText').val(function(i, v) {
              return `${v}Title: ${value.Game}\nCompany: ${value.Publisher}\nConsole: ${value.Platform.Console}\n\n`);
          });
        }
      });
    
      if (arr.length == 0) {
        $("#textAreaText").val("game not found");
      }
    }
    

    另外请注意,我使逻辑更简洁,因为您的两个条件几乎相同,您只需将 AND 逻辑放入 OR 语句中。

    【讨论】:

    • 哇,我不是唯一一个知道如何将函数参数用于像.val() 这样的设置器的人。
    • 感谢您的快速响应,但是当我使用您的代码时,我将 alert("hello") 放在函数 DisplayResults() 中,只是为了调试代码没有响应的原因,所以警报没有甚至弹出。所以我想我可能复制错了。
    • 听起来你在某个地方遇到了错误。查看控制台了解详情
    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多