当前位置:网站首页>JS form get form & get form elements
JS form get form & get form elements
2022-07-07 04:24:00 【Shuiyin 569】
Forms are a very common way for our pages to transfer data to the background , Sending data ( Request issued ) Before , We should now carry out a series of data validation on the page , Save unnecessary wrong data transmission , And improve the user experience .
1. Get the form
The first two are commonly used
1、document.getElementById("id Property value "); adopt form Labeled id Attribute value acquisition form
2、document. Form name Property value ; Through the form name Get form object from property value
3、document.forms[ Subscript ] Get form elements by specifying subscripts
4、document.forms[ Form name Property value ]; Through the form name Get form object from property value
document.forms: obtain HTML All form objects in the document
for example :
<body>
<form id='myform' name="myform" action=""method="post"></form>
<form id='myform2' name="myform2" action="" method="post"</form>
</body>
<script>
// Four ways
var form =document.getElementById("myform");
form =document.myform;
form =document.forms["myform"];
form =document.forms[0];
console.log(form);
</script>
2. Get form elements
2.1. obtain input Elements
Such as text password hidden textarea etc. , The first two are commonly used .
1. document.getElementById("id Property value "); By element id Property value to get the form element object
2. Form object . Of the form element name Property value ;
Through the of the corresponding element in the form object name Property value get
3. document.getELementsByName("name Property value "); Through the... Of form elements name Property value get
4. document.getELementsByTagName(" Tag name / Element name ");
Get the form element object by tag name
<body>
<form id='myform' name="myform" action="" method="get">
full name :<input type="text" id="uname" name="uname" value="zs"/><br />
password :<input type="password"id="upwd" name="upwd" value="1234"/><br />
<input type="hidden"id="uno" name="uno" value=" Hidden fields ”/>
Personal instructions :<textarea name="intro"></textarea>
<button type="button"onclick="getTxt();"> Get element content </button>
</form>
</body>
<script>
function getTxt()(
var uno = document.getElementById("uno");
var uname = myform.uname;
console.log(uname + “--------");
var upwd = document.getElementsByTagName('input')[1];
var intro = document.getElementsByName("intro")[0];
console.log(uno.value +", "+ uname.value +","+ upwd.value +", "+ intro.value);
</script>2.2. Get radio button
notes : The same set of radio buttons , You need to set the same name Property value
1. document.getElementByName("name Property value "); adopt name Property value get
2. Judge whether the radio button is selected
checked Selected state
stay JS In the code
checked=true Indicated selection
checked=false It means not to choose
stay HTML In the label
checked=checked or checked Indicated selection
Not set up checked attribute It means not to choose
3. Get the value of the radio button
Elements .value;
2.3. Get the multi-choice button
Same as radio button
2.4. Get drop-down options
1. Get the dropdown object
var object =document.getElementById("id Property value ”);
2. Get the drop-down option list of the drop-down box
var options = Drop down box object .options;
3. Get the index of the selected item in the drop-down box
var index = Drop down box object .selectedIndex;
4. Get the value of the selected item in the drop-down box
var value = Drop down box object .value;
5. Obtain the value of the selected item in the drop-down box through the subscript of the selected item
var value = Drop down box object .options[index].value;
6. Get the text of the selected item in the drop-down box
var Text value = Drop down box object .options[index].text;
notes :
1. When getting the value of the selected item in the drop-down box : (value)
If option The label is set value Property value , Then get value The value of the property ;
If option Label not set value Property value , What you get is option Text value in double label
2. The selected state of the drop-down box :
Selected state :selected selected、selected、selected=true
Unchecked state : Not set up selected attribute 、selected=false
边栏推荐
- 如何编写一个程序猿另一个面试官眼前一亮的简历[通俗易懂]
- Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
- Implementation of binary search tree
- EasyCVR视频广场点击播放时,主菜单高亮效果消失问题的修复
- Redis源码学习(31),字典学习,dict.c(一)
- golang 根据生日计算星座和属相
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- Collection of idea gradle Lombok errors
- Some thoughts on cross end development of kbone and applet
- ERROR: Could not build wheels for pycocotools which use PEP 517 and cannot be installed directly
猜你喜欢

Dab-detr: dynamic anchor boxes are better queries for Detr translation

kivy教程之设置窗体大小和背景(教程含源码)

Continuous learning of Robotics (Automation) - 2022-

【写给初发论文的人】撰写综述性科技论文常见问题

Surpassing postman, the new generation of domestic debugging tool apifox is elegant enough to use

Some thoughts on cross end development of kbone and applet

Opencv third party Library

数学分析_笔记_第10章:含参变量积分

ABAP dynamic inner table grouping cycle

【刷题记录】2. 两数相加
随机推荐
Unity3D在一建筑GL材料可以改变颜色和显示样本
Binary, octal, hexadecimal
Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency
什么是 CGI,什么是 IIS,什么是VPS「建议收藏」
2022 middle school Youth Cup mathematical modeling question B fertility policy research ideas under the background of open three children
The JSON format of the international area code of the mobile phone number is obtained with PHP
How do test / development programmers get promoted? From nothing, from thin to thick
UltraEdit-32 warm prompt: right association, cancel bak file [easy to understand]
PHP implements lottery according to probability
Redis source code learning (30), dictionary learning, dict.h
英特尔David Tuhy:英特尔傲腾技术成功的原因
【写给初发论文的人】撰写综述性科技论文常见问题
【刷题记录】2. 两数相加
VM virtual machine operating system not found and NTLDR is missing
Some common software related
【自动化经验谈】自动化测试成长之路
[on automation experience] the growth path of automated testing
Win11控制面板快捷键 Win11打开控制面板的多种方法
[ArcGIS tutorial] thematic map production - population density distribution map - population density analysis
2022年电工杯B 题 5G 网络环境下应急物资配送问题思路分析