表格名称无效 [英] Form name not working

查看:82
本文介绍了表格名称无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能这样做?

我正在传递表格的名称(我有两个使用此验证

脚本)但我一直得到一个错误。

错误读取:" document.which_form.name为null或不是对象


HTML ---------- -

表格是----> < form action =" thanks.php"方法= QUOT;交" name =" contact_form"

id =" contact_form">

名称-------> < input type =" text"命名= QUOT;名称" ID = QUOT;名称" size =" 25">

按钮发送代码-----> < input type =" button" value ="提交表格>

onClick =" contact_validate(''contact_form'')">


JAVASCRIPT ------ -----

函数contact_validate(which_form){

var field_errors ="需要以下字段:\ n\ n" ;;

var errors = 0;

//如果name为null

if(document.which_form [" name"]。value ==' '''){

// alert(请输入你的名字);

//返回;

field_errors = field_errors + 姓名 - 请输入你的姓名\ n;

错误++;

}


谢谢,

CWJ

解决方案

CJ写道:

为什么不能这样做?
我我传递了表单的名称(我有两个使用此验证
脚本)但我一直收到错误。
错误读取:document.which_form.name为null或不是对象

HTML -----------
表格是----> < form action =" thanks.php" method =" post"
name =" contact_form" id =" contact_form">
名称-------> < input type =" text"命名= QUOT;名称" ID = QUOT;名称" size =" 25">
按钮发送代码-----> < input type =" button" value ="提交表单>
onClick =" contact_validate(''contact_form'')">

JAVASCRIPT -----------
function contact_validate(which_form){
var field_errors ="需要以下字段:\ n \ n";
var errors = 0;
//如果名称为null
if(document.which_form [" name"]。value ==''''){
// alert(" Please enter your name");
//返回;
field_errors = field_errors +"名称 - 请输入您的姓名\ n";
错误++;
}

谢谢,
CWJ



1)将表单验证移动到< form>的onsubmit处理程序 - 你不需要

传递对表单的引用:


< form action =" thanks.php"方法= QUOT;交" onsubmit =" return contact_validate()">

< input type =" text"命名= QUOT;名称" ID = QUOT;名称" size =" 25" />

< input type =" submit" value =" Submit Form">

< / form>


2)更新验证功能


- 使用document.getElementById()引用''name''字段

- 如果有错误则返回false - 所以表单*不提交

- 如果没有错误则返回true - 所以表格*确实*提交


function contact_validate(){

var field_errors ="以下字段(s)是必需的:\ n \ n" ;;

var errors = 0;

//如果name为null

if (document.getElementById(''name'')。value ==''''){

field_errors = field_errors +"名称 - 请输入你的名字\ n" ;;

errors ++;

}

if(errors> 0){

alert(field_errors);

返回false;

}

返回true

}


谢谢,这就是诀窍。


我很欣赏帮助。


CWJ

" DB McGee" <无***** @ noreply.com>在消息中写道

新闻:fL ******************* @ news01.bloor.is.net.cab le.rogers.com .. 。

CJ写道:

为什么不能这样做?
我正在传递表格的名称(我有两个使用此验证
脚本)但我一直收到错误。
错误读取:" document.which_form.name为null或不是对象

HTML --------- -
表格是----> < form action =" thanks.php" method =" post"
name =" contact_form" id =" contact_form">
名称-------> < input type =" text"命名= QUOT;名称" ID = QUOT;名称" size =" 25">
按钮发送代码-----> < input type =" button" value ="提交表单>
onClick =" contact_validate(''contact_form'')">

JAVASCRIPT -----------
function contact_validate(which_form){
var field_errors ="需要以下字段:\ n \ n";
var errors = 0;
//如果名称为null
if(document.which_form [" name"]。value ==''''){
// alert(" Please enter your name");
//返回;
field_errors = field_errors +"名称 - 请输入您的姓名\ n";
错误++;
}

谢谢,
CWJ



1)将表单验证移动到< form>的onsubmit处理程序 - 你不需要
需要

传递对表单的引用:


< form action =" thanks.php"方法= QUOT;交" onsubmit =" return

contact_validate()">

< input type =" text"命名= QUOT;名称" ID = QUOT;名称" size =" 25" />

< input type =" submit" value =" Submit Form">

< / form>


2)更新验证功能


- 使用document.getElementById()引用''name''字段

- 如果有错误则返回false - 所以表单*不提交

- 如果没有错误则返回true - 所以表格*确实*提交


function contact_validate(){

var field_errors ="以下字段(s)是必需的:\ n \ n" ;;

var errors = 0;

//如果name为null

if (document.getElementById(''name'')。value ==''''){

field_errors = field_errors +"名称 - 请输入你的名字\ n" ;;

errors ++;

}

if(errors> 0){

alert(field_errors);

返回false;

}

返回true

}


< blockquote>" DB McGee" <无***** @ noreply.com>在消息中写道

新闻:fL ******************* @ news01.bloor.is.net.cab le.rogers.com .. 。

< snip>

1)将表单验证移动到< form>的onsubmit处理程序 -
你不需要传递对表格的引用:

< form action =" thanks.php" method =" post"
onsubmit =" return contact_validate()">


相反,从onsubmit

处理程序调用验证函数是匿名传递广义

验证的理想机会函数对FORM元素的引用是

应用于。在内部生成的事件处理函数中,使用onsubmit属性字符串创建了
, - this - keyword是

始终是对分配了函数的FORM元素的引用

作为一种方法: -


onsubmit =" return contact_validate(this);"


- 删除了对表单名称感兴趣的需要。

< input type =" text"命名= QUOT;名称" ID = QUOT;名称"大小= QUOT; 25 QUOT; />
< snip>


" name"可能是表单元素的错误名称,也可能是

元素的ID,因为W3C HTML DOM规范要求命名和/或

IDed属性可用作为FORM

元素的命名属性,因此上面的INPUT元素将作为

的属性提供,名称为name的形式。但是,FORM元素也需要

才能拥有名称为name的属性。表示表单的

名称。


实现可以选择用

a引用来替换表单的name属性名称为name的INPUT元素或者他们可能没有,

没有说明该决定的理由。通常,给出与FORM元素的现有

属性相对应的表单元素名称(或ID)是不明智的

,特别是如果这些属性正在进行中

将被使用。由于属性名称区分大小写,因此只需要
免除,将名称/ ID大写以避免冲突的风险。

2)更新验证功能

- 使用document.getElementById()


引用''name''字段
< snip>


如果函数的要点是提供一个通用函数使用

多个表单,并且该元素的ID将被硬编码在

函数中,那么策略将不起作用,因为getElementById

方法需要返回页面上第一个元素与

对应的ID,因此它将始终返回对INPUT的引用

元素ID为name在页面的第一个表单中。但是HTML要求

一个ID在页面上是唯一的,所以如果该函数打算用于超过<,那么将ID字符串硬编码到

函数中是不可行的br />
一个元素。


结果是,如果用于查找对

INPUT元素的引用的字符串是硬编码成一般验证函数

然后无法使用ID,因此getElementById是一种不合适的获取所需参考的方法。

鉴于需要解析对

表单中命名的INPUT元素的引用,其中相同的方法和名称字符串必须与几个

表单一起使用,查找输入元素引用的最佳位置是

作为 - elements - 表单集合的命名属性。一个

方法符合W3C HTML DOM规范,并且还与每个支持JavaScript的浏览器都支持
,这也是

了解什么是表格是: -


function contact_validate(formRef){

var field_errors ="需要以下字段:\ n\\\
" ;;

var errors = 0;

if(formRef.elements [''name''] .value ==''''){

field_errors = field_errors +"名称 - 请输入你的名字\ n" ;;

errors ++;

}

if(errors) > 0){

alert(field_errors);

返回false;

}

返回true

}


Richard。


Why won''t this work?
I am passing the name of the form (I have two that use this validation
script) but I keep getting an error.
Error reads: "document.which_form.name is null or not an object"

HTML-----------
Form is ----> <form action="thanks.php" method="post" name="contact_form"
id="contact_form">
Name -------> <input type="text" name="name" id="name" size="25">
Button sends code -----> <input type="button" value="Submit Form"
onClick="contact_validate(''contact_form'')">

JAVASCRIPT-----------
function contact_validate(which_form) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if (document.which_form["name"].value == '''') {
//alert("Please enter your name");
//return;
field_errors = field_errors+"Name - Please enter your name \n";
errors++;
}

Thanks,
CWJ

解决方案

CJ wrote:

Why won''t this work?
I am passing the name of the form (I have two that use this validation
script) but I keep getting an error.
Error reads: "document.which_form.name is null or not an object"

HTML-----------
Form is ----> <form action="thanks.php" method="post"
name="contact_form" id="contact_form">
Name -------> <input type="text" name="name" id="name" size="25">
Button sends code -----> <input type="button" value="Submit Form"
onClick="contact_validate(''contact_form'')">

JAVASCRIPT-----------
function contact_validate(which_form) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if (document.which_form["name"].value == '''') {
//alert("Please enter your name");
//return;
field_errors = field_errors+"Name - Please enter your name \n";
errors++;
}

Thanks,
CWJ


1) Move the form validation to the <form>''s onsubmit handler - you don''t need to
pass a reference to the form:

<form action="thanks.php" method="post" onsubmit="return contact_validate()">
<input type="text" name="name" id="name" size="25"/>
<input type="submit" value="Submit Form">
</form>

2) Update your validation function

- reference the ''name'' field using document.getElementById()
- return false if there are errors - so the form *doesn''t* submit
- return true if there are no errors - so the form *does* submit

function contact_validate() {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if ( document.getElementById( ''name'' ).value == '''') {
field_errors = field_errors+"Name - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}


Thanks, that did the trick.

I appreciate the help.

CWJ
"DB McGee" <no*****@noreply.com> wrote in message
news:fL*******************@news01.bloor.is.net.cab le.rogers.com...
CJ wrote:

Why won''t this work?
I am passing the name of the form (I have two that use this validation
script) but I keep getting an error.
Error reads: "document.which_form.name is null or not an object"

HTML-----------
Form is ----> <form action="thanks.php" method="post"
name="contact_form" id="contact_form">
Name -------> <input type="text" name="name" id="name" size="25">
Button sends code -----> <input type="button" value="Submit Form"
onClick="contact_validate(''contact_form'')">

JAVASCRIPT-----------
function contact_validate(which_form) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if (document.which_form["name"].value == '''') {
//alert("Please enter your name");
//return;
field_errors = field_errors+"Name - Please enter your name \n";
errors++;
}

Thanks,
CWJ


1) Move the form validation to the <form>''s onsubmit handler - you don''t
need to
pass a reference to the form:

<form action="thanks.php" method="post" onsubmit="return
contact_validate()">
<input type="text" name="name" id="name" size="25"/>
<input type="submit" value="Submit Form">
</form>

2) Update your validation function

- reference the ''name'' field using document.getElementById()
- return false if there are errors - so the form *doesn''t* submit
- return true if there are no errors - so the form *does* submit

function contact_validate() {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
// If name is null
if ( document.getElementById( ''name'' ).value == '''') {
field_errors = field_errors+"Name - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}


"DB McGee" <no*****@noreply.com> wrote in message
news:fL*******************@news01.bloor.is.net.cab le.rogers.com...
<snip>

1) Move the form validation to the <form>''s onsubmit handler -
you don''t need to pass a reference to the form:

<form action="thanks.php" method="post"
onsubmit="return contact_validate()">
On the contrary, calling the validation function from the onsubmit
handler is an ideal opportunity to anonymously pass a generalised
validation function a reference to the FORM element that it is to be
applied to. In the internally generated event handling function that is
created using the onsubmit attribute string the - this - keyword is
always a reference to the FORM element to which the function is assigned
as a method:-

onsubmit="return contact_validate(this);"

- Removes the need to be interested in the name of the form.
<input type="text" name="name" id="name" size="25"/> <snip>

"name" could be a bad name for a form element and also for the ID of an
element as the W3C HTML DOM specification requires that named and/or
IDed properties are made available as named properties of the FORM
element, so the above INPUT element would be available as a property of
the form under the name "name". However, the FORM element is also
required to have a property with the name "name" that represents the
name of the form.

Implementations may choose to replace the name property of the form with
a reference to the INPUT element with the name "name" or they might not,
the grounds for that decision are not specified. Generally it is unwise
to give form elements names (or IDs) that correspond with existing
properties of a FORM element, especially if those properties are going
to be used. As property names are case sensitive it is only necessary,
for exempt, to capitalise the name/ID to avoid the risk of conflicts.
2) Update your validation function

- reference the ''name'' field using document.getElementById()


<snip>

If the point of the function is to provide a general function for use
with multiple forms, and the ID of the element is to be hard coded in
the function then the strategy will not work as the getElementById
method is required to return the first element on the page with the
corresponding ID, so it will always return a reference to the INPUT
element IDed as "name" in the first form on the page. But HTML requires
that an ID be unique on the page so hard coding an ID string into a
function is not viable if the function is intended to act with more than
one element.

The result is that if the string used to look up the reference to an
INPUT element is to be hard coded into a general validation function
then an ID cannot be used, and so getElementById is an inappropriate
method of acquiring the required reference.

Given a need to resolve a reference to a named INPUT element within a
form, where the same method and name string must be used with several
forms, the best place to look-up the reference to the input element is
as a named property of the - elements - collection of the form. An
approach that conforms with the W3C HTML DOM specification and is also
back-compatible with every JavaScript capable browser that also
understands what a form is:-

function contact_validate(formRef) {
var field_errors = "The following field(s) are required: \n\n";
var errors = 0;
if ( formRef.elements[ ''name'' ].value == '''') {
field_errors = field_errors+"Name - Please enter your name \n";
errors++;
}
if ( errors > 0 ) {
alert( field_errors);
return false;
}
return true
}

Richard.


这篇关于表格名称无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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