简体   繁体   English

我想将 getElementByID 方法与 String Includes () 方法一起使用。 我怎样才能做到这一点?

[英]I want to use the getElementByID method with the String Includes () method. How can I do that?

On the website I'm making, I have this script that checks if a user has typed in a certain key phrase, and calls a function if the key phrase has been submitted.在我正在制作的网站上,我有这个脚本来检查用户是否输入了某个关键短语,如果已提交关键短语,则调用 function。

function onClick() {
                if (document.getElementById("user_input").value === "I hate the EU!")
                {
                    antiEuropeExample();
                }

Basically if the user input is exactly "I hate the EU;"基本上,如果用户输入正是“我讨厌欧盟”; the antiEuropeExample(). antiEuropeExample()。 function calls, I want to know if it's possible to have the function look for a keyword. function 调用,我想知道是否可以让 function 查找关键字。 so as long as the keyword is present the function will call, So.所以只要关键字存在 function 就会调用,所以。 instead of having to type exactly "I hate the EU!"而不必准确输入“我讨厌欧盟!” you could type "The EU sucks" and the function would still call.你可以输入“欧盟糟透了”,function 仍然会打电话。

I found this method online:我在网上找到了这个方法:

<script>
function myFunction() {
  var str = "Hello world, welcome to the universe.";
  var n = str.includes("world");
  document.getElementById("demo").innerHTML = n;
}
</script>

which seems promising, but I don't know how to combine the script I've already written and the str.includes method;这看起来很有希望,但我不知道如何将我已经编写的脚本和 str.includes 方法结合起来; I don't know if this is even possible, or if there's an easier way to do it.我不知道这是否可能,或者是否有更简单的方法。 It seems that in this example, you have to define the string yourself in the code, which doesn't help me because the user could type anything into the website (or perhaps I'm misunderstanding the code, I don't know,) I'm currently using just JavaScript, not JQuery.似乎在这个例子中,你必须在代码中自己定义字符串,这对我没有帮助,因为用户可以在网站中输入任何内容(或者我可能误解了代码,我不知道,)我目前只使用 JavaScript,而不是 JQuery。 if that helps.如果这有帮助。

Thank you so much for your help!非常感谢你的帮助!

In this example for your onClick function you would do exactly as the example you cited performed the check for the keyword.在此示例中,您的 onClick function 您将完全按照您引用的示例执行关键字检查。

An example of this in an if statement block would be as follows: if 语句块中的一个示例如下:

if(document.getElementById("user_input").value.includes("I hate the EU!"))
{
    antiEuropeExample();
}

This conditions returns a boolean of true or false depending on whether the string contained the phrase you passed in as a parameter.此条件返回 boolean 真或假,具体取决于字符串是否包含您作为参数传入的短语。 You would have to included && within your if statement to specify the minimum conditions for the block to execute eg the string must contain xyz, xyz, and xyz.您必须在 if 语句中包含 && 以指定执行块的最低条件,例如字符串必须包含 xyz、xyz 和 xyz。

Did you want something like this?你想要这样的东西吗?

 function onClick() { if (document.getElementById("user_input").value.includes("EU")) { //antiEuropeExample(); alert("ANTI EU;!!"); } }
 <input id="user_input"> <button onclick="onClick()">CLick:)</button>

Only a few info: .includes() is case sensitive, so example above will work only if you write EU , not if you will write eu .只有一些信息: .includes() 区分大小写,因此上面的示例仅在您编写EU时才有效,而不是在您编写eu时。

If you want to make it case unsensetive, try putting.toUpperCase() before.includes():如果您想让它不区分大小写,请尝试在.includes() 之前放置.toUpperCase():

 function onClick() { if (document.getElementById("user_input").value.toUpperCase().includes("EU")) { //antiEuropeExample(); alert("ANTI EU;!!"); } }
 <input id="user_input"> <button onclick="onClick()">CLick:)</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 html 变量上使用文档方法 getElementById() - How do I use Document method getElementById() on a html variable 不使用reverse()方法。 如何保持反向的原始字符串顺序,空格和标点符号? - Without using the reverse() method. How do I maintain the original string order, space and punctuation on string that was reverse? 当我单击此<a>链接时,它将发布回我的ASP.NET方法。</a> <a>我怎样才能做到这一点?</a> - When I click this <a> link it will post back to my ASP.NET method. How can I do that? 如何使用 lodash 中的 includes 方法检查对象是否在集合中? - How do I use the includes method in lodash to check if an object is in the collection? 我想在 get 方法中使用 promise 返回值。 nodejs休息api - I want to use a promise return value inside the get method. nodejs rest api 如何在 map 数组方法中加入逻辑。 我已将标题作为道具发送,除了道具标题我想呈现其他标题 - how can I put a logic in map array method. I have sent title as props and except props title I want to render other title 如何使用 JavaScript 中的 getElementById 方法为变量分配一个数字 - How can I assign a number for a variable by using getElementById method in JavaScript 我可以在 Javascript 的过滤器 function 中使用两个包含方法吗? - Can I use two includes method inside a filter function in Javascript? 使用eval为方法分配变量。 我该怎么办? - Using eval to assign a variable to a method. How else should I do it? 如何使用 Array includes() 方法检查数组中是否存在值? - How can i use Array includes() Method to check value exist in array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM