javascript 函数未被调用/无法从 html 表单工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18218096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:07:10  来源:igfitidea点击:

javascript function not being called/not working from html form

javascripthtmlformsfunction

提问by fonduman

This is my whole html page

这是我的整个 html 页面

<html>
<head>
<title> text conversion</title>
<script type="text/javascript">
function testResults(form)
{

var str = form.stringn.value;

str.split(" ");
document.write("testing1");
var length = str.length;
var newn = "";

for(var i = 0; i<length; i++)
{
    if(str[i].charAt(str[i].length - 1) != ".")
    {
        str[i] = str[i] + ".";
    }
    str[i] = str[i] + " ";
    str[i].charAt(0) = str[i].charAt(0).toUpperCase();
    newn = newn + str[i];

}
document.write(newn);
document.write("testing");
}


</script>
</head>

<body>
<form name="stringform" action="" method="get">
Text: <input type="text" name="stringn"><br>
<input type="button" name="Convert" Value="convert" onClick="testResults(this.form)">
</form>
</body>


</html>

The html is being displayed correctly, but the button does not produce any action. I even tried document.write("testing") below the loop in the javascript, which had no effect, which makes me think the function is not being called at all. The idea of the function is to format the input string to capitalise the first letter of each word, and put a period after each word.

html 显示正确,但按钮不产生任何动作。我什至在 javascript 中的循环下方尝试了 document.write("testing") ,但没有效果,这让我认为该函数根本没有被调用。该函数的思想是将输入字符串格式化为每个单词的第一个字母大写,并在每个单词后放置一个句点。

This is my first time trying to use javascript, so possibly I'm doing something wrong that should be obvious?

这是我第一次尝试使用 javascript,所以可能我做错了什么应该很明显?

final solution:

最终解决方案:

<html>
<head>
<title> text conversion</title>
<script type="text/javascript">
function testResults(form)
{

var str = form.stringn.value;

var strn = str.split(" ");

var length = strn.length;
var newn = "";

for(var i = 0; i<length; i++)
{

    if(strn[i].charAt(strn[i].length - 1) != ".")
    {
        strn[i] = strn[i] + ".";
    }
    strn[i] = strn[i].charAt(0).toUpperCase() + strn[i].slice(1);

    strn[i] = strn[i] + " ";
    newn = newn + strn[i];


}
document.write(newn);

}


</script>
</head>

<body>
<form name="stringform" action="" method="get">
Text: <input type="text" name="stringn"><br>
<input type="button" name="Convert" Value="convert" onClick="testResults(this.form)">
</form>
</body>


</html>

回答by Nate Jenson

This should work. Also, echoing The comments before about using new as a variable.

这应该有效。另外,回应之前关于使用 new 作为变量的评论。

<html>
<head>
<title> text conversion</title>
<script language="JavaScript">

function testResults(form)
{
    var str = form.stringn.value;
    var newString = "";
    var strList = str.split(" ");
    for(var i = 0; i < strList.length; i++){
        newWord = strList[i].charAt(0).toUpperCase() + strList[i].slice(1) + ".";       
        newString += newWord + " ";
    }
    document.write(newString);
}

</script>
</head>

<body>
<form name="stringform" action="" method="get">
Text: <input type="text" name="stringn"><br>
<input type="button" name="Convert" Value="convert" onClick="testResults(this.form)">
</form>
</body>


</html>

回答by Deepak Biswal

Set <script type="text/javascript">in your <head>!

设置<script type="text/javascript">在您的<head>!