为什么是“未定义”单击“警报”消息框上的“确定”后出现? [英] why is "undefined" appearing after I click OK on an Alert messagebox?

查看:59
本文介绍了为什么是“未定义”单击“警报”消息框上的“确定”后出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习javascript,并创建了一个基本的.htm页面,其中包含3个调用javascript函数的按钮 - 我看到的图像和下面的源代码。第一个按钮调用一个名为sayHi()的函数,它只会显示一条警告消息,上面写着
"Hi ..."。但当我在该警报消息上单击"确定"时,我看到"未定义"第二个按钮上方.htm页面上的消息。当我点击第二个按钮时,我得到时间和日期。第三个按钮使用带有javascript的ADODB从Adventureworks数据库中检索10行
的数据(我意识到使用带有ADODB的JS并不安全,这是一个不好的做法,......这只是用于试验JS)。数据库中的数据显示正确,但数据显示在自己的窗口中。有没有办法让
来自AdventureWorks的数据出现在呈现按钮的同一页面上?我的意思是有办法在我的.htm文件中做到这一点吗?怎么做?或者我们现在在asp.net国家? 我猜这是一个AJAX的事情。 AJAX只能在Asp.Net中使用吗?

I am practicing with javascript and created a basic .htm page with 3 buttons that call javascript functions -- images of what I see and the source code below. The first button calls a function called sayHi() which just brings up an alert message that says "Hi..." but when I click OK on that alert message I see an "undefined" message on the .htm page just above the 2nd button. When I click the 2nd button I get the time and Date. The third button uses ADODB with javascript to retrieve 10 rows of data from Adventureworks database (I realize that using JS with ADODB is not secure, a bad practice, ... this is just for experimenting with JS). The data from the database appears correctly, but the data appears in its own window. Is there a way to make the data from AdventureWorks appear on the same page that renders the buttons? I mean is there a way to do it in my .htm file? How to do that? or are we now in asp.net country?  I will guess this is an AJAX thing. Does AJAX only work in Asp.Net?

SayHi致电

点击OK后我得到"未定义"第一个按钮下方的消息。 有没有办法防止"未定义"?出现?

after I click OK I get this "undefined" message below the first button.  Is there a way to prevent that "undefined" from showing up?

第二个按钮似乎工作正常 - 显示日期&时间

the 2nd buttons appears to work fine -- show date & time

第三个按钮从AdventureWorks获取数据并显示正常,但数据显示在自己的页面中。 有没有办法在第三个按钮下显示?

the third button gets data from AdventureWorks and displays it fine, but the data is displayed in its own page.  Is there a way to make display under the third button?

这里是所有图片的源代码

and here is the source code for all the images

<html>
<head>
<title> testing JS </title>

    <script>
	function getData()
	{
       var objConnection = new ActiveXObject("adodb.connection");
       var strConn = "Provider=sqloledb;Data Source=win7;Initial Catalog=AdventureWorks;Integrated Security=SSPI;";
       objConnection.Open(strConn);
       var rs = new ActiveXObject("ADODB.Recordset");
       var strQuery = "SELECT Top 10 * FROM  Person.Address";
       rs.Open(strQuery, objConnection);
       rs.MoveFirst();
       while (!rs.EOF) {
          document.write(rs.fields(0) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
          document.write(rs.fields(1) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
          document.write(rs.fields(2) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    ");
          document.write(rs.fields(3) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    ");
          document.write(rs.fields(4) + "<br/>");
          rs.movenext();
        }
	}
	
	function sayHi()
	{
	    alert("hi there and welcome");
	}
    </script>
</head>

<body>

<h1>JavaScript Testing</h1>

<button type="button" onclick="document.getElementById('demo1').innerHTML = sayHi()">
Click me to say hi.</button>
<p id="demo1"></p>

<button type="button" onclick="document.getElementById('demo2').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo2"></p>

<button type="button" onclick="document.getElementById('demo3').innerHTML = getData()">
Click me to get data.</button>
<p id="demo3"></p>

</body>
</html>

Rich P

推荐答案

亲爱的,

由于第一个按钮中的以下代码,您的第一个HTML元素显示未定义

Your first HTML element says Undefined because of the following code in the first button

onclick="document.getElementById('demo1').innerHTML = sayHi()"

它的作用:它将demo1内部html设置为函数"sayHi"的返回值。然而,功能"sayHi"从不返回任何价值。它只执行命令并退出。

进一步学习:如果要从函数返回值,则必须退出功能

return <value> ;

声明。

在你的它应该是这样的:

function sayHi() {
alert("hi there and welcome");
return "success";
}


请注意,如果您需要返回代码。函数中的所有流程(如果Else If,Else,Switch,case,default等)应返回一个值。




希望它能帮助你学习!

问候,

Remco Vermeer



Remco Vermeer


这篇关于为什么是“未定义”单击“警报”消息框上的“确定”后出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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