0

我已经制作了一个表格,当你按下 go 时,你在文本框中输入的内容会出现在屏幕上,只是它不起作用,这里是 JavaScript 代码: var sentence1; 变种句子2;var 中间句;

function paraFunction(setence1, middlesentence, setence2)
{
sentence1 = "You plan is to ";
sentence2 = " and you must succeed at all costs";
middlesentence = form.strPara.value;
paraShow();
}

function paraShow()
{
document.getElementById('paraAns').innerHTML = (sentence1 + middlesentence + sentence2);
}

这是HTML代码:

<div id="innerbody">
</div>
<h1>Testing Parameters</h1>
<p>In this example, I will be testing parameters, below you will see a form, asking you to enter a sentence, little do you know, that when you click on the form button, a sentence will be completed below, with your sentence in the middle... oops!</p>
<form type="paraForm" method="get">
<input type="text" placeholder="Input your sentence here" id="strPara"/>
<input type="button" value="Go!" name="paraFunction" onClick="paraFunction(this.form);"/>
</form>
<p id="paraAns"></p>
</div>
4

3 回答 3

0

您需要将这些句子变量传递给paraShow

function paraFunction(setence1, middlesentence, setence2) {
    sentence1 = "You plan is to ";
    sentence2 = " and you must succeed at all costs";
    middlesentence = form.strPara.value;
    paraShow(sentence1, middlesentence, sentence2);
}

function paraShow(sentence1, middlesentence, sentence2) {
    document.getElementById('paraAns').innerHTML = (sentence1 + middlesentence + sentence2);
}
于 2013-03-07T16:37:11.047 回答
0

这段代码有很多问题。首先,您的 paraFunction 声明和您的调用具有不同的参数。其次,您将按钮命名为 paraFunction(这可能不是重大更改,只是令人困惑)。第三,您尝试使用 form.strPara 访问 strPara,因为您没有名为 form 的表单,所以这不起作用。一旦清除了这些,代码就可以正常工作:

function paraFunction() {
    sentence1 = "You plan is to ";
    sentence2 = " and you must succeed at all costs";
    middlesentence = document.getElementById('strPara').value;
    paraShow();
}

function paraShow() {
    document.getElementById('paraAns').innerHTML = (sentence1 + middlesentence + sentence2);
}

和 HTML:

<div id="innerbody">
</div>
<h1>
    Testing Parameters</h1>
<p>
    In this example, I will be testing parameters, below you will see a form, asking
    you to enter a sentence, little do you know, that when you click on the form button,
    a sentence will be completed below, with your sentence in the middle... oops!</p>
<form type="paraForm" method="get">
<input type="text" placeholder="Input your sentence here" id="strPara" />
<input type="button" value="Go!" name="paraFun" onclick="paraFunction();" />
</form>
<p id="paraAns">
</p>
</div>
于 2013-03-07T16:55:48.247 回答
0

问题在于范围和函数调用的方式。看评论

function paraFunction(setence1, middlesentence, setence2)
{
    /* The function is called as 'paraFunction(this.form)' */
    /* At this time the value of the three arguments will be */
    /* setence1 = this.form */
    /* middlesentence = setence2 = undefined */

    /* This will create a variable 'sentence1' in global scope */
    /* name of the local variable is setence1 and not sentence1 */
    sentence1 = "You plan is to ";

    /* This will create a variable 'sentence2' in global scope */ 
    sentence2 = " and you must succeed at all costs"; 

    /* variable middlesentence is in local scope. won't be accessible outside */
     /* form is not defined in this scope, it should have been setence1 */
     middlesentence = form.strPara.value; 
     paraShow();
}

function paraShow()
{
 /*sentence1 and sentence2 will be accessible to this function but not middlesentence */        
 document.getElementById('paraAns').innerHTML = (sentence1 + middlesentence + sentence2);
}

有多种方法可以解决您的问题

更改 paraFunction 中第二个参数的名称。(说中间句)。虽然这是一个糟糕的解决方案,因为应该尽量减少全局变量的使用(在窗口范围内,如果是浏览器)。此外,在您不使用的函数中传递参数也是没有意义的。

function paraFunction(setence1, middlesetence, setence2)
{
    sentence1 = "You plan is to ";
    sentence2 = " and you must succeed at all costs";
    middlesentence = setence1.strPara.value;
    paraShow();
 }

 function paraShow()
 {
     document.getElementById('paraAns').innerHTML = (sentence1 + middlesentence + sentence2);
 }

修改这两个函数以接受正确的参数并像这样使用局部变量。

function paraFunction(form)
{
    var sentence1 = "You plan is to ";
    var sentence2 = " and you must succeed at all costs";
    var middlesentence = form.strPara.value;
    paraShow(sentence1,middlesentence,sentence2);
 }

 function paraShow(sentence1,middlesentence,sentence2)
 {
     document.getElementById('paraAns').innerHTML = (sentence1 + middlesentence + sentence2);
 }

您还需要更改按钮的名称。

于 2013-03-07T17:00:26.033 回答