如何在同一页上打印值 [英] how to print value in the same page

查看:63
本文介绍了如何在同一页上打印值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我吗?

我已经在javascript中编写了一个函数:

Can anybody please help me?

I have written a function in javascript:

<script language="JavaScript">
    var count = 0;
    function setText(){
        if(document.check.jawapan.value == "A"){
            alert("Tahniah. Jawapan anda betul");
            count = 1;
        }
        else
        {
            alert("Jawapan anda salah. Sila cuba lagi");
            count = 0;
        }
        document.write(count);
    }
</script>




我已经创建了运行该功能的链接.




I have created a link to run the function.

<input type=BUTTON value="Semak" name="mySubmit" önClick="setText()">



单击按钮后,该值将打印在新页面上,而不是在同一页面上.我错过了一些代码还是有人可以建议我做错了什么?

在此先感谢您!



After I click the button the value prints on the new page and not in same page. Did I miss some code or can anybody suggest what I''m doing wrong?

Thanks in advance!

推荐答案

document.write(...)将完全执行您所观察的操作.它将一些文本写入文档主体,如果该主体已经加载,则将导致该主体被完全覆盖.如果在加载文档时执行内联脚本功能,则该文档中的行为将大不相同.write将添加到已加载的文本中.
这种行为一直在引起混乱,但是现在您知道了,它不再是问题.
:)

欢呼和快乐的编码!
document.write(...) will do exactly what you are observing. It writes some text into to the document body and if the body had already been loaded this will cause the body to be completely overwritten. If one executes an inline script function while the document is being loaded the behaviour is quite different in that document.write will add to the text already loaded.
This behavior has always been causing confusion, but now that you know about it it should no longer be an issue.
:)

Cheers and happy coding!



为什么要使用document.write打印计数值?

您可以在HEAD
中写以下内容
Hi
Why are you print Count value using document.write?

you can write following in HEAD
<script language="JavaScript">
    var count = 0;
    function setText(){
        if(document.check.jawapan.value == "A"){
            alert("Tahniah. Jawapan anda betul");
            count = 1;
        }
        else
        {
            alert("Jawapan anda salah. Sila cuba lagi");
            count = 0;
        }
        document.getElementById('divOutput').innerHTML = count;
    }
</script>



您可以在HTML正文中使用以下内容



you can use as following in HTML body

<div id="divOutput"></div>
<input type="button" value="semak"  önclick="setText()" />


这篇关于如何在同一页上打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆