如何将文本框值发送到javascript函数?

时间:2022-12-01 16:40:31

I seem to be doing something wrong here. This script works with the prompts, which have been commented out, but not with the textboxes. Am I somehow failing to send the input values to the function?

我好像在做错事。此脚本适用于已注释掉但不包含文本框的提示。我是否以某种方式未能将输入值发送给函数?

I'm also having trouble using regular expressions in the if-statements, rather than a clumsy list of punctuation marks.

我也在if语句中使用正则表达式时遇到了麻烦,而不是一个笨拙的标点符号列表。

<html>
<head>
</head>
<body>
<div id="myTypingText"></div>
<label>Say what you want typed</label> 
<input class="textBox" id="userInput" />
<label>A pregnant pause... (300?)</label>
<input type="number" id="userBreath" />
<button onclick="printLooper()" href="javascript:;">Submit</button>
<! --input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" --/>
<script type="text/javascript" language="javascript">
var myString = document.getElementById('userInput').value;
//var myString = prompt('Say what you want to see typed','Right here, baby'); //prompts are annoying, i know
//var myDelay = prompt('Type speed (try 50)', 'The higher the number, the slower it types');
//var myBreath = prompt('Now tell me how long to pause at each breath (shift+2)', 'Try 300')
var myBreath = document.getElementById('userBreath').value;
var myArray = myString.split("");
var loopTimer;

function printLooper(){
  if(myArray.length > 0 ){
      var char = myArray.shift();
      if ( char === '@'){
          document.getElementById("myTypingText").innerHTML
      }else {
      document.getElementById("myTypingText").innerHTML += char;
      }
  } else {
    clearTimeout(loopTimer);
  }
  if (char === ' '){
  loopTimer = setTimeout('printLooper()', 20);
  } else if (char === ',' || char === '.' || char === '?') {
  loopTimer = setTimeout('printLooper()', 220); //fiddle with these 2nd params as you see fit
  } else if (char === '@'){
  loopTimer = setTimeout('printLooper()', myBreath);
  } else {
  loopTimer = setTimeout('printLooper()', 47);
  }
}
printLooper(); 

</script>
</body>
</html>

Any help appreciated!

任何帮助赞赏!

4 个解决方案

#1


2  

You need to have some variables inside your function.

你需要在函数中包含一些变量。

I made a demo and removed the inline js on the button. Use this if useful:

我做了一个演示并删除了按钮上的内联js。如果有用,请使用此:

<button id="sub_btn">Submit</button>


var loopTimer;
var char, myString, myBreath, myArray;

function printLooper() {
    if (myArray.length > 0) {
        char = myArray.shift();
        console.log(char);
        if (char === '@') {
            document.getElementById("myTypingText").innerHTML = '';
        } else {
            document.getElementById("myTypingText").innerHTML += char;
        }
    } else {
        clearTimeout(loopTimer);
        return false; // To the loop will stop when the array is empty
    }
    if (char === ' ') {
        loopTimer = setTimeout(printLooper, 20);
    } else if (char === ',' || char === '.' || char === '?') {
        loopTimer = setTimeout(printLooper, 220); //fiddle with these 2nd params as you see fit
    } else if (char === '@') {
        loopTimer = setTimeout(printLooper, myBreath);
    } else {
        loopTimer = setTimeout(printLooper, 47);
    }
}
document.getElementById('sub_btn').onclick = function () {
    myString = document.getElementById('userInput').value;
    myBreath = document.getElementById('userBreath').value;
    myArray = myString.split("");
    printLooper(myString, myBreath, myArray);
};

Demo here

#2


0  

This syntax has extra characters you probably typo'd in there. I also have never seen a button element with the href attribute before but I don't get out much. You might want to revisit this.

此语法具有您可能在其中拼写错误的额外字符。我之前从未见过带有href属性的按钮元素,但我没有得到太多。您可能想重温这一点。

<button onclick="printLooper()" href="javascript:;">Submit</button>

#3


0  

You should put these lines inside the function

你应该将这些行放在函数中

var myString = document.getElementById('userInput').value;
var myBreath = document.getElementById('userBreath').value;
var myArray = myString.split("");

It's empty because you put it outside the function and initially the inputs are empty and inside the function if(myArray.length > 0 ) is always false, you need to populate the array once the function is called after the button pressed and also href is not a valid attribute for button as another answer stated it.

它是空的,因为你把它放在函数之外,最初输入是空的,如果(myArray.length> 0)总是假的,你需要在按下按钮后调用函数时填充数组,并且href是另一个答案表明,它不是按钮的有效属性。

#4


0  

var myString = document.getElementById('userInput').value;
//var myString = prompt('Say what you want to see typed','Right here, baby'); //prompts are annoying, i know
//var myDelay = prompt('Type speed (try 50)', 'The higher the number, the slower it types');
//var myBreath = prompt('Now tell me how long to pause at each breath (shift+2)', 'Try 300')
var myBreath = document.getElementById('userBreath').value;
var myArray = myString.split("");

Move these inside the function.

在功能内移动它们。

#1


2  

You need to have some variables inside your function.

你需要在函数中包含一些变量。

I made a demo and removed the inline js on the button. Use this if useful:

我做了一个演示并删除了按钮上的内联js。如果有用,请使用此:

<button id="sub_btn">Submit</button>


var loopTimer;
var char, myString, myBreath, myArray;

function printLooper() {
    if (myArray.length > 0) {
        char = myArray.shift();
        console.log(char);
        if (char === '@') {
            document.getElementById("myTypingText").innerHTML = '';
        } else {
            document.getElementById("myTypingText").innerHTML += char;
        }
    } else {
        clearTimeout(loopTimer);
        return false; // To the loop will stop when the array is empty
    }
    if (char === ' ') {
        loopTimer = setTimeout(printLooper, 20);
    } else if (char === ',' || char === '.' || char === '?') {
        loopTimer = setTimeout(printLooper, 220); //fiddle with these 2nd params as you see fit
    } else if (char === '@') {
        loopTimer = setTimeout(printLooper, myBreath);
    } else {
        loopTimer = setTimeout(printLooper, 47);
    }
}
document.getElementById('sub_btn').onclick = function () {
    myString = document.getElementById('userInput').value;
    myBreath = document.getElementById('userBreath').value;
    myArray = myString.split("");
    printLooper(myString, myBreath, myArray);
};

Demo here

#2


0  

This syntax has extra characters you probably typo'd in there. I also have never seen a button element with the href attribute before but I don't get out much. You might want to revisit this.

此语法具有您可能在其中拼写错误的额外字符。我之前从未见过带有href属性的按钮元素,但我没有得到太多。您可能想重温这一点。

<button onclick="printLooper()" href="javascript:;">Submit</button>

#3


0  

You should put these lines inside the function

你应该将这些行放在函数中

var myString = document.getElementById('userInput').value;
var myBreath = document.getElementById('userBreath').value;
var myArray = myString.split("");

It's empty because you put it outside the function and initially the inputs are empty and inside the function if(myArray.length > 0 ) is always false, you need to populate the array once the function is called after the button pressed and also href is not a valid attribute for button as another answer stated it.

它是空的,因为你把它放在函数之外,最初输入是空的,如果(myArray.length> 0)总是假的,你需要在按下按钮后调用函数时填充数组,并且href是另一个答案表明,它不是按钮的有效属性。

#4


0  

var myString = document.getElementById('userInput').value;
//var myString = prompt('Say what you want to see typed','Right here, baby'); //prompts are annoying, i know
//var myDelay = prompt('Type speed (try 50)', 'The higher the number, the slower it types');
//var myBreath = prompt('Now tell me how long to pause at each breath (shift+2)', 'Try 300')
var myBreath = document.getElementById('userBreath').value;
var myArray = myString.split("");

Move these inside the function.

在功能内移动它们。