[英]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.