如何在 上使用 onFocus、onChange 和 onClick 事件场地
我有一个家庭作业,我应该在三个表单字段中的每一个字段上使用 onFocus
、onChange
和 onClick
事件。每个事件都应该将字段名称传递给函数。该功能应该警告事件已经发生,并让用户知道发生了多少次警报。上周我一直在尝试寻找答案,但我始终能够发现 onFocus 事件不能与文本字段一起使用(但这不会改变我的作业)。到目前为止我的代码如下:
<SCRIPT LANGUAGE="JavaScript">
<!--
var numEvents = 0;
var field1 = "";
var field2 = "";
var field3 = "";
function clickedField(fieldId) {
if (document.form1.field1.value = field1){
events=runningTotal(1);
alert("You have clicked Field 1. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field2.value = field2){
events=runningTotal(1);
alert("You have clicked Field 2. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field3.value = field3){
events=runningTotal(1);
alert("You have clicked Field 3. Alert Count = " + runningTotal(i) + ".");
}
}
function changedField(fieldId) {
if (document.form1.field1.value!= field1){
events=runningTotal(1);
alert("You have changed Field 1. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field2.value!= field2){
events=runningTotal(1);
alert("You have changed Field 2. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field3.value!= field3){
events=runningTotal(1);
alert("You have changed Field 3. Alert Count = " + runningTotal(i) + ".");
}
}
/*
function focusedField(fieldId) {
if (document.form1.field1.value = field1){
events=runningTotal(1);
alert("You have focused on Field 1. Alert Count = " + runningTotal(i) + ".");
}
else if (document.form1.field2.value = field2){
events=runningTotal(1);
alert("You have focused on Field 2. Alert Count = " + runningTotal(i) + ".");
}
else if (document.form1.field3.value = field3){
events=runningTotal(1);
alert("You have focused on Field 3. Alert Count = " + runningTotal(i) + ".");
}
}*/
function runningTotal(i){
numEvents += i;
return numEvents;
}
// -->
</SCRIPT>
我知道有几个错误,但实际代码中没有,因为它没有做我需要它做的事情。在我添加 之前。 Alert Count = " + runningTotal(i) + "."
以及当我更改字段时它告诉我的警报参数。
I have a homework assignment where I am supposed to use the onFocus
, onChange
and onClick
events on each of three form fields. Each event is supposed to pass the field name to the function. The function is supposed to alert that the event has taken place and let the user know how many alerts have occurred. I have spent the last week trying to find the answer and all I have been able to consistently find is that the onFocus
event is NOT to be used with text fields (that does not change my assignment though). The code I have so far is as follows:
<SCRIPT LANGUAGE="JavaScript">
<!--
var numEvents = 0;
var field1 = "";
var field2 = "";
var field3 = "";
function clickedField(fieldId) {
if (document.form1.field1.value = field1){
events=runningTotal(1);
alert("You have clicked Field 1. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field2.value = field2){
events=runningTotal(1);
alert("You have clicked Field 2. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field3.value = field3){
events=runningTotal(1);
alert("You have clicked Field 3. Alert Count = " + runningTotal(i) + ".");
}
}
function changedField(fieldId) {
if (document.form1.field1.value!= field1){
events=runningTotal(1);
alert("You have changed Field 1. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field2.value!= field2){
events=runningTotal(1);
alert("You have changed Field 2. Alert Count = " + runningTotal(i) + ".");
}
if (document.form1.field3.value!= field3){
events=runningTotal(1);
alert("You have changed Field 3. Alert Count = " + runningTotal(i) + ".");
}
}
/*
function focusedField(fieldId) {
if (document.form1.field1.value = field1){
events=runningTotal(1);
alert("You have focused on Field 1. Alert Count = " + runningTotal(i) + ".");
}
else if (document.form1.field2.value = field2){
events=runningTotal(1);
alert("You have focused on Field 2. Alert Count = " + runningTotal(i) + ".");
}
else if (document.form1.field3.value = field3){
events=runningTotal(1);
alert("You have focused on Field 3. Alert Count = " + runningTotal(i) + ".");
}
}*/
function runningTotal(i){
numEvents += i;
return numEvents;
}
// -->
</SCRIPT>
I know there are several errors though not in the actual code because it is not doing what I need it to do. Before I added the . Alert Count = " + runningTotal(i) + "."
and the argument to the alert it was telling me when I changed a field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定您是否需要以下代码来触发这些 javascript 函数
I'm not sure you need the folloing code to trigger those javascript functions
需要是
Need to be
首先,我在代码中没有看到将这些函数分配给必要事件的任何地方。
其次,我没有在任何地方看到
field1
、field2
和field3
变量被分配了值。除此之外,您正在检查该字段的值,无论如何,我不确定您为什么要这样做。第三,您必须使用
==
来测试等效性,而不是=
。First, I don't see anywhere in your code that you assign those functions to the necessary events.
Second, I don't see anywhere that the
field1
,field2
, andfield3
vars are assigned to a value. Along with that, you are checking the value of the field, and I'm not sure why you are doing that, anyway.And third, you have to use
==
to test for equivalency, not=
.首先要做的事情是:您的 if 语句需要比较运算符 (==) 而不是赋值运算符 (=),之后
类似的操作将起作用:
它将向函数发送对 itelf 的引用,您可以在其中访问其值或 id 等。
它需要在 window.onload 之后处理,以确保表单项在事件分配之前存在。
first things first: your if statements need comparison operators (==) instead of assignment operators (=), after that
something like this would work:
It would send a reference to itelf to the function, where you can access its value or id etc.
It needs to be handled after window.onload to ensure the form items exist before the event is assigned.