Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript的文本框中查找单词_Javascript_Html - Fatal编程技术网

如何在JavaScript的文本框中查找单词

如何在JavaScript的文本框中查找单词,javascript,html,Javascript,Html,我现在正在制作一个与谷歌应用程序类似的网站,我想知道如何让JavaScript在文本框中查找某个单词。例如,如果有人在文本框中输入“Show me the weather”,JavaScript世界会看到关键字weather,它会向我显示天气。这是我的密码: JavaScript: function showAlert() { var txtCtrl = document.getElementById("textbox1"); var txtVal = txtCtrl.val

我现在正在制作一个与谷歌应用程序类似的网站,我想知道如何让JavaScript在文本框中查找某个单词。例如,如果有人在文本框中输入“Show me the weather”,JavaScript世界会看到关键字weather,它会向我显示天气。这是我的密码:

JavaScript:

function showAlert() {

    var txtCtrl = document.getElementById("textbox1");

    var txtVal = txtCtrl.value;

    if (txtVal == '') {
        alert('Please fill in the text box. For a list of commands type "Help" into the text box.');
    }else if (txtVal == 'Start' {
        alert('Hello. What would you like me to do?');
    }else if (txtVal === 'Weather') {
        window.location = "https://www.google.com/#q=weather";
    }else if (txtVal === 'Time') {
        alert('The current time according to your computer is' + formatTime(new Date()));
    }else if (txtVal === 'Help') {
        window.location = "help/index.html";
    }else if (txtVal === 'Donate') {
        window.location = "donate/index.html";
    }else if (txtVal === 'WWW.' ||) {

    }else{
        alert('Sorry, I do not reconise that command. For a list of commands, type "Help" into the text box.');
    }
}
//Show time in 24hour format
function showTime(){
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes(); 
  return [ h, m ].join(':')
}
//Show time in 12hour format
var formatTime = (function () {
    function addZero(num) {
        return (num >= 0 && num < 10) ? "0" + num : num + "";
    }

    return function (dt) {
        var formatted = '';

        if (dt) {
            var hours24 = dt.getHours();
            var hours = ((hours24 + 11) % 12) + 1;
            formatted = [formatted, [addZero(hours), addZero(dt.getMinutes())].join(":"), hours24 > 11 ? "PM" : "AM"].join(" ");            
        }
        return formatted;
    }
})();
函数showAlert(){
var txtCtrl=document.getElementById(“textbox1”);
var txtVal=txtCtrl.value;
如果(txtVal==''){
警报('请填写文本框。有关命令列表,请在文本框中键入“帮助”。);
}else if(txtVal=='Start'{
警惕(‘你好,你想让我做什么?’);
}否则如果(txtVal==='Weather'){
window.location=”https://www.google.com/#q=weather";
}else if(txtVal==='Time'){
警报('根据您的计算机,当前时间为'+formatTime(新日期()));
}else if(txtVal==='Help'){
window.location=“help/index.html”;
}否则如果(txtVal===“捐赠”){
window.location=“捐赠/index.html”;
}else if(txtVal=='WWW.| |){
}否则{
警报('抱歉,我没有重新识别该命令。有关命令列表,请在文本框中键入“帮助”。);
}
}
//以24小时格式显示时间
函数showTime(){
var today=新日期();
var h=today.getHours();
var m=today.getMinutes();
返回[h,m]。联接(':')
}
//以12小时格式显示时间
var formatTime=(函数(){
函数addZero(num){
返回(num>=0&&num<10)?“0”+num:num+”;
}
返回函数(dt){
var格式=“”;
if(dt){
var hours24=dt.getHours();
变量小时=((小时24+11)%12)+1;
格式化=[格式化,[addZero(hours),addZero(dt.getMinutes())]。加入(“:”,小时24>11?”下午:“:”上午]。加入(“”;
}
返回格式;
}
})();
以及HTML:

<!doctype html>
<html>
<head>
<title>Random Project</title>
</head>
<body>
<div class="container">
    <img class="logo" src="logo.png" width="450" height="110" alt="Random Project">
    <input type="text" name="textbox1" value="" spellcheck="false" dir="ltr" placeholder="Type here" id="textbox1"><br>
    <button id="button1" name="button1" aria-label="Help me" onClick="showAlert();">
        <span id="button1_text">Help me</span>
    </button>
    <div class="separator"></div>
    <span class="information">&copy; Copyright DemDevs 2013. All rights reserved. Made by Omar Latreche<br><a href="donate/index.html">Donate now</a></span>
    <div class="tip">
        <span class="tip">Tip: </span><span class="tip_text">The commands are NOT case sensitive</span>
    </div>
</div>
<div class=""></div>
</body>
</html>

随机项目

帮助我 &副本;版权所有DemDevs 2013。保留所有权利。由Omar Latreche制作
提示:这些命令不区分大小写
任何帮助都将不胜感激


谢谢,Omar!

您可以检查字符串是否包含子字符串。使用
indexOf
函数,它将返回给定字符串在其父字符串中的位置。请尝试以下操作:

if (txtVal == '') {
    alert('Please fill in the text box. For a list of commands type "Help" into the text box.');
} else if (txtVal.indexOf("start") != -1) {
    alert('Hello. What would you like me to do?');
} else if (txtVal.indexOf("weather") != -1) {
    window.location = "https://www.google.com/#q=weather";
}
// and so on.
如果找不到start值,则函数返回-1

而且。如果你有很多这样的函数,你应该把所有的函数词放到数组中,然后避免使用太大的If/else系统。然后你应该检查数组,如果存在相应的函数。我制作函数是为了帮助你的项目:

function returnWord(value, array) {
    if (!(value in array)) {
        return array[value]();
    }
    else {
        alert('returnWord() error: No needle found in haystack');
    }
}
可以这样称呼:

var words = {
             start: function() { alert('Hello. What would you like me to do?'); },
             weather: function() { window.location = "https://www.google.com/#q=weather"; },
             time: function() { alert('The current time according to your computer is' + formatTime(new Date())); },
             /* all the functions here */
            };

returnWord(txtVal, words);

您可以检查字符串是否包含子字符串。使用
indexOf
函数,它将返回给定字符串在其父字符串中的位置。请尝试以下操作:

if (txtVal == '') {
    alert('Please fill in the text box. For a list of commands type "Help" into the text box.');
} else if (txtVal.indexOf("start") != -1) {
    alert('Hello. What would you like me to do?');
} else if (txtVal.indexOf("weather") != -1) {
    window.location = "https://www.google.com/#q=weather";
}
// and so on.
如果找不到start值,则函数返回-1

而且。如果你有很多这样的函数,你应该把所有的函数词放到数组中,然后避免使用太大的If/else系统。然后你应该检查数组,如果存在相应的函数。我制作函数是为了帮助你的项目:

function returnWord(value, array) {
    if (!(value in array)) {
        return array[value]();
    }
    else {
        alert('returnWord() error: No needle found in haystack');
    }
}
可以这样称呼:

var words = {
             start: function() { alert('Hello. What would you like me to do?'); },
             weather: function() { window.location = "https://www.google.com/#q=weather"; },
             time: function() { alert('The current time according to your computer is' + formatTime(new Date())); },
             /* all the functions here */
            };

returnWord(txtVal, words);

很简单,你可以这样做

var str = document.getElementById("textbox1");
var n = str.indexOf("welcome");

if(n != -1){
 //Do what you want
}else{
 //Get Out
}

很简单,你可以这样做

var str = document.getElementById("textbox1");
var n = str.indexOf("welcome");

if(n != -1){
 //Do what you want
}else{
 //Get Out
}

使用也可以使用
search()
方法:

...
if (txtVal.search("start") != -1) {
   alert('Hello. What would you like me to do?');
}
...
请点击此处:

使用也可以使用
search()
方法:

...
if (txtVal.search("start") != -1) {
   alert('Hello. What would you like me to do?');
}
...
请点击此处:

使用
match
indexOf
。同时查看开关功能以避免出现if/else jungleHow触发功能?阅读上述内容,然后尝试:
/\bYOUR\u WORD\b/
我认为这可能会对您有所帮助。欢迎使用
match
indexOf
。同时查看开关功能以避免if/else jungleHow you's trigger your function?请阅读以上内容,然后尝试:
/\bYOUR\u WORD\b/
我想这可能会对您有所帮助。干杯