带有JavaScript的HTML表单输入标签名称元素数组 [英] HTML form input tag name element array with JavaScript

查看:107
本文介绍了带有JavaScript的HTML表单输入标签名称元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个两部分问题。有人在前一天回答了类似的问题(其中也包含有关PHP中这种类型的数组的信息),但我找不到它。

首先,是在表单的输入标签的name元素的末尾创建的数组的正确术语?

 < form> ; 

< input name =p_id []value =0/>
< input name =p_id []value =1/>
< input name =p_id []value =2/>

< / form>



<2>。)如何从JavaScript中获取该数组的信息?具体来说,我现在只想计算数组的元素。

$ p $ function form_check(){
for(var i = 0; i< count(document.form.p_id []); i ++){//错误在这行

if(document.form.p_name [i] .value =='') {
window.alert('名称消息');
document.form.p_name [i] .focus();
休息;


else {
if(document.form.p_price [i] .value ==''){
window.alert('Price Message') ;
document.form.p_price [i] .focus();
休息;
}

else {
update_confirmation();
}
}
}
}


解决方案


1。)首先,在表单的输入标签的名称元素的末尾创建的数组的正确术语是什么?


Oftimes混淆PHPism



就JavaScript而言,一大堆表单控件具有相同名称的只是一些具有相同名称的表单控件,而包含方括号的表单控件只是名称包含方括号的表单控件。



具有相同名称的表单控件的PHP命名规则有时很有用(当你有许多控件组时,你可以这样做:

 < input name =name [1]> 
< input name =email [1]>
< input name =sex [1 ]type =radiovalue =m>
< input name =sex [1]type =radiovalue =f>

< ;一世输入名称=名称[2]>
< input name =email [2]>
< input name =sex [2]type =radiovalue =m>
< input name =sex [2]type =radiovalue =f>

)但是会混淆一些人。其他一些语言已经采用了惯例,因为这是最初编写的,但通常仅作为可选功能。例如,通过这个JavaScript模块


<2>。)如何使用JavaScript从该数组中获取信息?

它仍然只是一个从元素获取与窗体控件同名的属性的问题。诀窍是由于表单控件的名称包含方括号,因此不能使用点符号和必须使用方括号表示法,就像任何其他包含特殊字符的JavaScript属性名称。



由于您有多个具有该名称的元素,它将是一个集合,而不是一个控件,所以你可以使用它的length属性循环它的循环。

  var myForm = document.forms .id_of_form; 
var myControls = myForm.elements ['p_id []'];
for(var i = 0; i< myControls.length; i ++){
var aControl = myControls [i];
}


This is a two part question. Someone answered a similar question the other day (which also contained info about this type of array in PHP), but I cannot find it.

1.) First off, what is the correct terminology for an array created on the end of the name element of an input tag in a form?

<form>

    <input name="p_id[]" value="0"/>
    <input name="p_id[]" value="1"/>
    <input name="p_id[]" value="2"/>

</form>

2.) How do I get the information from that array with JavaScript? Specifically, I am right now just wanting to count the elements of the array. Here is what I did but it isn't working.

function form_check(){
    for(var i = 0; i < count(document.form.p_id[]); i++){  //Error on this line

        if (document.form.p_name[i].value == ''){
            window.alert('Name Message');
            document.form.p_name[i].focus();
            break;
        }

        else{
            if (document.form.p_price[i].value == ''){
                window.alert('Price Message');
                document.form.p_price[i].focus();
                break;
            }

            else{
                update_confirmation();
            }
        }
    }
}

解决方案

1.) First off, what is the correct terminology for an array created on the end of the name element of an input tag in a form?

"Oftimes Confusing PHPism"

As far as JavaScript is concerned a bunch of form controls with the same name are just a bunch of form controls with the same name, and form controls with names that include square brackets are just form controls with names that include square brackets.

The PHP naming convention for form controls with the same name is sometimes useful (when you have a number of groups of controls so you can do things like this:

<input name="name[1]">
<input name="email[1]">
<input name="sex[1]" type="radio" value="m">
<input name="sex[1]" type="radio" value="f">

<input name="name[2]">
<input name="email[2]">
<input name="sex[2]" type="radio" value="m">
<input name="sex[2]" type="radio" value="f">

) but does confuse some people. Some other languages have adopted the convention since this was originally written, but generally only as an optional feature. For example, via this module for JavaScript.

2.) How do I get the information from that array with JavaScript?

It is still just a matter of getting the property with the same name as the form control from elements. The trick is that since the name of the form controls includes square brackets, you can't use dot notation and have to use square bracket notation just like any other JavaScript property name that includes special characters.

Since you have multiple elements with that name, it will be a collection rather then a single control, so you can loop over it with a standard for loop that makes use of its length property.

var myForm = document.forms.id_of_form;
var myControls = myForm.elements['p_id[]'];
for (var i = 0; i < myControls.length; i++) {
    var aControl = myControls[i];
}

这篇关于带有JavaScript的HTML表单输入标签名称元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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