Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

再次了解 Form 元素 #19

Open
fisker opened this issue Dec 14, 2018 · 0 comments
Open

再次了解 Form 元素 #19

fisker opened this issue Dec 14, 2018 · 0 comments

Comments

@fisker
Copy link
Owner

fisker commented Dec 14, 2018

今天读了一些关于 html form 元素的文档, 记录几点

form[method]

因为在 http 规范中 POST GET PUT 等方法都是大写的,我一直以为 method 也应该用大写,实际上按照规范应该是小写

https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method

虽然他们大小写不敏感,但还是按照规法比较好,实际上不管你写大写还是小写 form.method 都会是小写的。同样 button[formmethod] 也应该小写
我想到去看下这个是因为我最近用到了 method="dialog"

form.enctype

form.enctype 在 html5 中新增了一个 text/plain, 搜了一圈也没找到具体增加这个的原因,可能真的如找到的几篇文章所言也就 form[action^="mailto:"] 的时候有点用吧

indeterminate

input[type="checkbox"] indeterminate 不会影响 checkbox 的提交, 只和 checked 有关。
如果要提交 indeterminate 状态,可以创建一个 input[type="hidden"] 来保存

form[name]

规范 https://html.spec.whatwg.org/multipage/forms.html#htmlformelement

我以前不知道的几点

  1. form[name] 的类型

nameid 访问 form control 元素的时候, 和 input 的 type 无关。
只和元素的个数有个 只要是多个元素 就会返回 RadioNodeList(即使他们的type不是radio),如果只有一个元素,就会返回 HTMLElement(即使input的type是radio) 。

  1. 当你用 form[name].value 获取 选中的 radio 的值的时候可能不对

因为上一条的原因,如果表单里面只有一个同名的 radio 那么这里可能会踩到坑 那就是这个radio可能并没有被选中。因为只有一个元素的时候返回的是 HTMLInputElement 一般都会有value

  1. form[name] 可能是一个 img

当你访问的name没有 form control 元素,但是有一个img[id="name"]时,你会得到一个<img>元素,其他具有同样id的元素则不会

  1. 即使 inputidname 改变,你仍然可以用原来的 idname 访问到这个input, 直到这个元素被移除
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant