简体   繁体   English

调用Javascript函数处理表单后,HTML表单不打印到浏览器

[英]HTML form not printing to browser after calling Javascript function to process form

<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>

The result I expect is javascript to execute after i click on the button and then display html form...but html form isn't displaying afterwards... 我期望的结果是我点击按钮然后显示html表单后执行javascript ...但是之后没有显示html表单...

Any help very much appreciated, thanks! 非常感谢任何帮助,谢谢!

The output i want is: 我想要的输出是:

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>

What are you trying to accomplish? 你想达到什么目的? Display the text under or above the form? 在表格下方或上方显示文字? Document.Write will actually override the entire content of the document when it is not run inline during load. Document.Write实际上会在加载期间未内联运行时覆盖文档的整个内容。 So to do what you want to do you need to create a container to put the text into and then place the text in there. 因此,要执行您想要执行的操作,您需要创建一个容器来放置文本,然后将文本放在那里。 So for example : 例如:

<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>

Then the javascript would be : 然后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>

A better way to do this would be to use a JavaScript framework called JQuery(http://jquery.com). 更好的方法是使用名为JQuery(http://jquery.com)的JavaScript框架。 It provides easy ways to attach event handlers and manipulate contents. 它提供了附加事件处理程序和操作内容的简便方法。 Removing inline javascript is good for maintenance. 删除内联JavaScript有利于维护。

You could try this: 你可以试试这个:

<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>

The div is used to display the ouput. div用于显示输出。 The div and the fields of the form are both selected by their id. 表格的div和字段都是由他们的id选择的。

If you want to be sure that the user does not enter a string, use this javascript: 如果您想确保用户没有输入字符串,请使用以下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!";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM