简体   繁体   English

JavaScript:区分表单属性和表单字段

[英]JavaScript: Distinguish between a Form Property and a Form Field

Given a simple form: 给出一个简单的形式:

<form id="data" action="">
    <input type="text" name="id">
    <input type="text" name="info">
    <button type="submit" name="ok">
</form>

I can read the properties and form fields as follows: 我可以读取属性和表单字段,如下所示:

var data=document.querySelector['form#data'];
alert(data.action);
alert(data['action']);
alert(data.info);
alert(data['info']);
alert(data['id']);
alert(data.id);

Now, for the most part, form['info'] is an alternative for form.info . 现在,在大多数情况下, form['info']form.info的替代form.info However I prefer to use form['info'] for form fields and form.info for actual properties. 但是我更喜欢将form['info']用于表单字段,并将form.info用于实际属性。

The question is: in JavaScript, how can I determine whether form['info'] (or form.info ) is a form field or a form property? 问题是:在JavaScript中,如何确定form['info'] (或form.info )是表单字段还是表单属性? I know it is from the form above, but how would I know without knowing about a specific form? 我从上面的表格知道它,但是在不知道特定表格的情况下我怎么知道?

Supplementary to that, if I have a form field named the same as a property (such as id above), which takes precedence? 作为补充,如果我有一个与属性相同的表单字段(例如上面的id ),则优先使用?

OK, I have the answer. 好,我有答案。

  1. In the case of data['id'] or data.id , the form field takes precedence over the form property. 对于data['id']data.id ,form字段优先于form属性。 This can be problematic, so it might be safer to use form.getAttribute('id') to be safe. 这可能会出现问题,因此使用form.getAttribute('id')可能更安全。

  2. Because of the confusion, it would be better to always use data.elements . 由于混淆,最好始终使用data.elements That only contains the form's elements, so that's easy. 包含窗体的元素,所以这是很容易。

I will answer the question based on my understanding of your question. 我将根据对您问题的理解回答该问题。

In the code given by you: 在您提供的代码中:

<form id="data" action="">
    <input type="text" name="id">
    <input type="text" name="info">
    <button type="submit" name="ok">
</form>

If you do a query selector data=document.querySelector('form#data'); 如果您执行查询选择器data=document.querySelector('form#data');

And then if you try to access data.id 然后,如果您尝试访问data.id

Your Question is 您的问题是

  • Would the output be the "id attribute of the form" or "the child node/object named id" 输出将是“表单的id属性”还是“名为id的子节点/对象”

  • Which one takes the precedence 哪一个优先

The output would be the node/object named "id" and not the ID attribute of the form. 输出将是名为“ id”的节点/对象,而不是表单的ID属性。 The purpose of DOM is to traverse the object model tree, hence a node takes preference over an attribute/property. DOM的目的是遍历对象模型树,因此节点优先于属性/属性。

To know if the returned output is an attribute or not: 要知道返回的输出是否是属性:

  • Attribute returns a text. 属性返回一个文本。 That's the endpoint. 那是终点。
  • HTML Element returns an object (which can again be used to traverse the DOM tree) HTML元素返回一个对象(可以再次用于遍历DOM树)

I think I understand your question: how to determine whether a form property references a standard property of an HTML form element or a form control, eg: 我想我理解您的问题:如何确定表单属性是引用HTML表单元素的标准属性还是表单控件,例如:

<form name="formName">
  <input name="inputName">

so that: 以便:

form.name

is a property of the form that will return a string, whereas: 是将返回字符串的形式的属性,而:

form.inputName

is a property that will return a form control element (or NodeList for radio buttons). 是将返回表单控件元素(或单选按钮的NodeList)的属性。

The short answer is you can't just from the name, since form properties can be shaddowed by same-named form controls, eg 简短的答案是您不能仅仅使用名称,因为表单属性可以被同名的表单控件所掩盖,例如

<input type="submit" name="submit">

means you can't call form.submit() because now form.submit references the submit button, not the method. 意味着您不能调用form.submit()因为现在form.submit引用了提交按钮,而不是方法。

You might consider ducktyping, so: 您可能会考虑鸭嘴式,因此:

var thing = form.blah;
if (typeof thing == 'string' || typeof thing == 'function') {
   // probably a property of the form itself

} else if {thing == form[thing.name]) {
  // probably a form control
}

and so on. 等等。 But in javascript it's normally not a good idea to do that, far better to just look for the feature you want to use and if available, use it. 但是用javascript通常不是一个好主意,要查找要使用的功能(如果可用)就更好了。 Say you want to get the value: 假设您想获得价值:

if (form.blah && typeof form.blah.value == 'string') {
  // do stuff with form.blah.value
}

But typically you know enough about the form's properties (eg by using suitable naming conventions for controls and properties and avoiding known standard form properties like "submit", "reset", "action", "name" etc.) to not have to do that. 但是通常您对表单的属性了解得足够多(例如,通过对控件和属性使用适当的命名约定,并避免使用已知的标准表单属性,例如“提交”,“重置”,“动作”,“名称”等),不必这样做那。

So the bottom line is always use sensible names for form controls that do not clash with standard form property names, then you can tell from the name if it's a form property or control name. 因此,对于与标准表单属性名称不冲突的表单控件,最重要的是始终使用明智的名称,然后您可以从名称中分辨出它是表单属性还是控件名称。 One scheme is to always use camel case and two word names, so rather than "name", use "userName" or "employeeName", etc. For submit button, use "submitButton", for reset, use "resetButton", though it's not common to name them. 一种方案是始终使用驼峰式大小写和两个单词名称,因此,不是“名称”,而是“ userName”或“ employeeName”等。对于提交按钮,请使用“ submitButton”,对于重置,请使用“ resetButton”,尽管命名它们并不常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM