问题是
if语句检查:
document.CityChoice.City[0].checked == true
这与Javascript不完全一样,因为那里的错误表达式会引发 错误 ,并且永远不会使它进入
if语句主体。
我删除了该
if语句,并将代码更改为如下所示:
function selectedCity(){ document.SimpleMessageApplet.setMessage("Hello");}
当我单击时,我会看到Hello消息很好。
将您的HTML文件内容更改为以下内容:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <script LANGUAGE="Javascript"> function selectedCity() { var elem = document.getElementById('cityRb'); if(elem.checked) { document.SimpleMessageApplet.setMessage(elem.value); } } </script></HEAD> <BODY > <b>This is the Applet</b> <APPLET CODE="MessageApplet.class" NAME="SimpleMessageApplet" WIDTH=350 HEIGHT=100 > </APPLET > <FORM NAME="CityChoice"> <input type="radio" id="cityRb" name="City" value="Boston" onClick="selectedCity()"> Boston<br> </form></BODY ></html>
同时添加完整的类代码:
import java.applet.Applet;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Label;public class MessageApplet extends Applet { private Label m_mess; public void init() { setBackground(Color.lightGray); setLayout(new BorderLayout()); m_mess = new Label("MessageApplet is Running...: No Selection Yet", Label.CENTER); add(BorderLayout.CENTER, m_mess); m_mess.setBackground(Color.red); } public void setMessage(String message) { m_mess.setText("Selection: " + message); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)