查找表单输入的第一个单词

Finding the first word of a form input

本文关键字:第一个 单词 输入 表单 查找      更新时间:2023-09-26

所以我正在做一个个人项目,到目前为止已经提出了这个项目(见下文),我想知道,与其用if语句来表示"aatrox成本"和传奇联盟中的其他冠军,我想知道是否可以查看搜索中是否有成本,并从搜索中提取第一个单词(在本例中为aatrox),并将其放入"_cost.length"answers"_cost[counter]"中

else if(document.getElementById("search_data").value=="aatrox cost") {
    for (counter=0; counter<aatrox_cost.length; counter++)
        document.getElementById("search_result").innerHTML+=(aatrox_cost[counter] + "<br />")
}

是的,这个项目是基于传奇联盟的:D

看看IndexOf,这可能就是您正在寻找的

http://www.w3schools.com/jsref/jsref_indexof.asp

试试这样的东西:

var name;
var search_data = document.getElementById("search_data").value;
if(search_data.search("cost") != -1) {
    name = search_data.split(" ")[0];    
}

变量名称应为search_data中的第一个单词。