如何在 上使用 onFocus、onChange 和 onClick 事件场地

发布于 2024-12-25 15:12:14 字数 2169 浏览 9 评论 0原文

我有一个家庭作业,我应该在三个表单字段中的每一个字段上使用 onFocusonChangeonClick 事件。每个事件都应该将字段名称传递给函数。该功能应该警告事件已经发生,并让用户知道发生了多少次警报。上周我一直在尝试寻找答案,但我始终能够发现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衬遮猫 2025-01-01 15:12:15

我不确定您是否需要以下代码来触发这些 javascript 函数

<input name="field1" id="field1" onfocus="focusedField(this.id)" onclick="clickedField(this.id)" onchange="changedField(this.id)"/>
<input name="field2" id="field2" onfocus="focusedField(this.id)" onclick="clickedField(this.id)" onchange="changedField(this.id)"/>
<input name="field3" id="field3" onfocus="focusedField(this.id)" onclick="clickedField(this.id)" onchange="changedField(this.id)"/>

I'm not sure you need the folloing code to trigger those javascript functions

<input name="field1" id="field1" onfocus="focusedField(this.id)" onclick="clickedField(this.id)" onchange="changedField(this.id)"/>
<input name="field2" id="field2" onfocus="focusedField(this.id)" onclick="clickedField(this.id)" onchange="changedField(this.id)"/>
<input name="field3" id="field3" onfocus="focusedField(this.id)" onclick="clickedField(this.id)" onchange="changedField(this.id)"/>
三五鸿雁 2025-01-01 15:12:15
if (document.form1.field1.value = field1){ 

需要是

if (document.form1.field1.value == field1){ 
if (document.form1.field1.value = field1){ 

Need to be

if (document.form1.field1.value == field1){ 
泪痕残 2025-01-01 15:12:15

首先,我在代码中没有看到将这些函数分配给必要事件的任何地方。

其次,我没有在任何地方看到 field1field2field3 变量被分配了值。除此之外,您正在检查该字段的值,无论如何,我不确定您为什么要这样做。

第三,您必须使用 == 来测试等效性,而不是 =

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, and field3 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 =.

乄_柒ぐ汐 2025-01-01 15:12:15

首先要做的事情是:您的 if 语句需要比较运算符 (==) 而不是赋值运算符 (=),之后

类似的操作将起作用:

window.onload = function(){
    document.form1.field1.onclick = function(){
        changedField(this);
    }
}

它将向函数发送对 itelf 的引用,您可以在其中访问其值或 id 等。
它需要在 window.onload 之后处理,以确保表单项在事件分配之前存在。

first things first: your if statements need comparison operators (==) instead of assignment operators (=), after that

something like this would work:

window.onload = function(){
    document.form1.field1.onclick = function(){
        changedField(this);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文