0
<script type="text/javascript">

function gr8r( form ) {

document.write( '"' + form.s1.value + '"' );    

if ( form.s1.value > form.s2.value ) 
    document.write( ' is gr8r than ' );
else
    document.write( ' is not gr8r than ' );

document.write( '"' + form.s2.value + '"' );


}

</script>

<form action="" method="post">

<input type="text" name="s1" value="" />
<input type="text" name="s2" value="" />
<br />
<input type="button" value="click" onclick="gr8r(this.form)" />

</form>

我期望的结果是在我单击按钮然后显示 html 表单后执行 javascript ......但之后没有显示 html 表单......

非常感谢任何帮助,谢谢!

我想要的输出是:

2 is gr8r than 1

<form action="" method="post">

<input type="text" name="s1" value="" />
<input type="text" name="s2" value="" />
<br />
<input type="button" value="click" onclick="gr8r(this.form)" />

</form>
4

2 回答 2

1

你想达到什么目的?在表单下方或上方显示文本?Document.Write 在加载期间未内联运行时,实际上会覆盖文档的全部内容。因此,要执行您想做的事情,您需要创建一个容器来将文本放入其中,然后将文本放入其中。例如:

<div id="message"></div>
<form action="" method="post">

<input type="text" name="s1" value="" />
<input type="text" name="s2" value="" />
<br />
<input type="button" value="click" onclick="gr8r(this.form)" />

</form>

那么javascript将是:

<script type="text/javascript">

function gr8r( form ) {

message = document.getElementById("message");
msgStr = '"' + form.s1.value + '"';

if ( form.s1.value > form.s2.value ) 
    msgStr += ' is gr8r than ';
else
    msgStr += ' is not gr8r than ';

msgStr += '"' + form.s2.value + '"';

message.innerHTML = msgStr;

}

</script>

更好的方法是使用名为 JQuery(http://jquery.com) 的 JavaScript 框架。它提供了附加事件处理程序和操作内容的简单方法。删除内联 javascript 有利于维护。

于 2012-05-30T18:22:05.970 回答
1

你可以试试这个:

<script type="text/javascript">
function gr8r( form ) {
    document.getElementById("output").innerHTML = document.getElementById("s1").value;

    if ( document.getElementById("s1").value > document.getElementById("s2").value ) 
        document.getElementById("output").innerHTML += ' is gr8r than ';
    else
        document.getElementById("output").innerHTML += ' is not gr8r than ';

    document.getElementById("output").innerHTML += document.getElementById("s2").value;
}
</script>

<div id="output"></div>
<form action="" method="post">
    <input type="text" id="s1" value="" />
    <input type="text" id="s2" value="" />
    <br />
    <input type="button" value="click" onclick="gr8r(this.form)" />
</form>

div 用于显示输出。div 和表单的字段都由它们的 id 选择。

如果您想确保用户没有输入字符串,请使用以下 javascript:

function gr8r( form ) {
    if (isFinite(document.getElementById("s1").value) && isFinite(document.getElementById("s2").value)) {
        document.getElementById("output").innerHTML = document.getElementById("s1").value;

        if (document.getElementById("s1").value > document.getElementById("s2").value) 
            document.getElementById("output").innerHTML += ' is gr8r than ';
        else
            document.getElementById("output").innerHTML += ' is not gr8r than ';

        document.getElementById("output").innerHTML += document.getElementById("s2").value;
    }
    else {
        document.getElementById("output").innerHTML = "Please enter only floats!";
    }
}
于 2012-05-30T18:24:17.957 回答