Home Web Front-end JS Tutorial Bootstrap form component tutorial detailed explanation_javascript skills

Bootstrap form component tutorial detailed explanation_javascript skills

May 16, 2016 pm 03:03 PM

Common elements of forms mainly include: text input box, drop-down selection box, radio button, check box, text field, button, etc. Here are the different bootstrap versions:

LESS: forms.less

SASS: _forms.scss

Bootstrap only customizes the fieldset, legend, and label tags in the form

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

fieldset {

min-width: 0;

padding: 0;

margin: 0;

border: 0;

}

legend {

display: block;

width: 100%;

padding: 0;

margin-bottom: 20px;

font-size: 21px;

line-height: inherit;

color: #333;

border: 0;

border-bottom: 1px solid #e5e5e5;

}

label {

display: inline-block;

margin-bottom: 5px;

font-weight: bold;

}

Copy after login

In addition to these elements, there are also input, select, textarea and other elements. In the bootstrap framework, the effect is achieved by customizing a class name .form-control

1. The width becomes 100%;

2. Set a light gray (#ccc) border

3. Rounded corners with 4px

4. Set the shadow effect, and when the element gets focus, the shadow and border effects will change

5. Set the color of the placeholder to #999

Inline form

If you want to add a label before the input, it will cause the input to wrap in another line. If you have to add such a label and don’t want the input to wrap, you need to put the label in the container.form-group. For example:

1

2

3

4

5

6

<div class="form-group ">

<label class="sr-only">邮箱地址</label>

</div>

<div class="form-group">

<input type="email" class="form-control" placeholder="请输入邮箱号">

</div>

Copy after login

The effect is as follows:

To achieve the inline form effect, just add the class name .form-inline to the form element. Implementation principle:

Set the form control as an inline block element (display: inline-block) to display the form control in one line.

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<form class="form-inline">

<div class="form-group">

<label class="sr-only">邮箱</label>

<input class="form-control" type="email" placeholder="请输入邮箱号">

</div>

<div class="form-group">

<label class="sr-only">密码</label>

<input type="password" class="form-control" placeholder="请输入密码">

</div>

<div class="checkbox">

<label>

<input type="checkbox" > 记住密码

</label>

</div>

<div class="form-group">

<button class="btn btn-default">进入邮箱</button>

</div>

</form>

Copy after login

The effect is as follows:

Seeing the effect in the above picture, have you noticed that there is a label in the code, and it is not placed in the container.form-group, and the input does not wrap into new lines. What is even more strange is that the content of the label is not displayed! In fact, if you look closely at the label label, the class name .sr-only is added, which hides the label. Let’s take a look at its source code:

1

2

3

4

5

6

7

8

9

10

.sr-only {

position: absolute;

width: 1px;

height: 1px;

padding: 0;

margin: -1px;

overflow: hidden;

clip: rect(0, 0, 0, 0);

border: 0;

}

Copy after login

Now that the label tag is added, and the .sr-only class name is added to hide the label, is it unnecessary? ? ? But this is exactly one of the advantages of the bootstrap framework. If the label is not set for the input control, the screen reader will not be able to recognize it correctly. At the same time, certain considerations have been made for people with disabilities

Horizontal form

To achieve the horizontal form effect in bootstrap, the following two conditions must be met:

1. Use the class name .form-horizontal
on the form element

2. Grid system with bootstrap framework (Details: Detailed explanation of Bootstrap grid system)

Using the class name .form-horizontal in the form element mainly has the following functions:

1. Set padding and margin values ​​of form controls

2. Change the expression of .from-group, similar to row in the grid system

css source code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

.form-horizontal .control-label,

.form-horizontal .radio,

.form-horizontal .checkbox,

.form-horizontal .radio-inline,

.form-horizontal .checkbox-inline {

padding-top: 7px;

margin-top: 0;

margin-bottom: 0;

}

.form-horizontal .radio,

.form-horizontal .checkbox {

min-height: 27px;

}

.form-horizontal .form-group {

margin-right: -15px;

margin-left: -15px;

}

.form-horizontal .form-control-static {

padding-top: 7px;

}

@media (min-width: 768px) {

.form-horizontal .control-label {

text-align: right;

}

}

.form-horizontal .has-feedback .form-control-feedback {

top: 0;

right: 15px;

}

Copy after login

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<form class="form-horizontal">

<div class="form-group">

<label class="col-sm-2 control-label">邮箱</label>

<div class="col-sm-10">

<input type="email" class="form-control" placeholder="请输入邮箱">

</div>

</div>

<div class="form-group">

<label class="col-sm-2 control-label">密码</label>

<div class="col-sm-10">

<input type="password" class="form-control" placeholder="请输入密码">

</div>

</div>

<div class="form-group">

<div class="col-sm-10 col-sm-offset-2">

<label>

<input type="checkbox">记住密码

</label>

</div>

</div>

<div class="form-group">

<div class="col-sm-10 col-sm-offset-2">

<button class="btn btn-default">进入邮箱</button>

</div>

</div>

</form>

Copy after login

The effect is as follows:

Single line input box

When using input in bootstrap, you must also add the type type. If the type type is not specified, you will not be able to get the correct style, because the bootstrap framework defines the style in the form of input[type="?"], such as : text type, corresponding to input[type="text"]

In order to make the control look good in various form styles, you need to add the class name .form-control

1

2

3

4

5

<form role="form">

<div class="form-group">

<input type="email" class="form-control" placeholder="enter email" >

</div>

</form>

Copy after login

Drop-down selection box select

Multiple row selection sets the value of the multiple attribute to multiple

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<form role="form">

<div class="form-group">

<select class="form-control">

<option>1</option>

<option>2</option>

<option>3</option>

<option>4</option>

<option>5</option>

</select>

</div>

<div class="form-group">

<select multiple class="form-control">

<option>1</option>

<option>2</option>

<option>3</option>

<option>4</option>

<option>5</option>

</select>

</div>

</form>

Copy after login

Text area textarea

The text area is used in the same way as the original one. Setting rows can define its height, and setting cols can define its width. If the class name .form-control is added to the textarea element, there is no need to set the cols attribute, because the .form in the bootstrap framework The target space width of -control style is 100% or auto

1

2

3

4

5

<form role="form">

<div class="form-group">

<textarea class="form-control" rows="3"></textarea>

</div>

</form>

Copy after login

复选框checkbox和单选框radio

checkbox和radio与label标签配合使用会出现一些小问题(如对齐问题)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<form>

<div class="checkbox">

<label>

<input type="checkbox"> 记住密码

</label>

</div>

<div class="radio">

<label>

<input type="radio" name="optionsRadios" id="optionsRadios1" checked> 喜欢

</label>

</div>

<div class="radio">

<label>

<input type="radio" name="optionsRadios" id="optionsRadios2">不喜欢

</label>

</div>

</form>

Copy after login

1、不管是checkbox还是radio都使用label包起来了

2、checkbox连同label标签放在一个名为.checkbox的容器内

3、radio连同label标签放在一个名为.radio的容器内,bootstrap主要借助.checkbox和.radio样式来处理复选框、单选按钮与标签的对齐方式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

.radio,

.checkbox {

display: block;

min-height: 20px;

padding-left: 20px;

margin-top: 10px;

margin-bottom: 10px;

}

.radio label,

.checkbox label {

display: inline;

font-weight: normal;

cursor: pointer;

}

.radio input[type="radio"],

.radio-inline input[type="radio"],

.checkbox input[type="checkbox"],

.checkbox-inline input[type="checkbox"] {

float: left;

margin-left: -20px;

}

.radio + .radio,

.checkbox + .checkbox {

margin-top: -5px;

}

Copy after login

复选框和单选按钮水平排列

1、如果checkbox需要水平排列,只需要在label标签上添加类名.checkbox-inline

2、如果radio需要水平排列,只需在label标签上添加类名.radion-inline

下面是css源码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

.radio-inline,

.checkbox-inline {

display: inline-block;

padding-left: 20px;

margin-bottom: 0;

font-weight: normal;

vertical-align: middle;

cursor: pointer;

}

.radio-inline + .radio-inline,

.checkbox-inline + .checkbox-inline {

margin-top: 0;

margin-left: 10px;

}

<div class="form-group">

<label class="radio-inline">

<input type="radio" name="sex"value="option1"> 男性

</label>

<label class="radio-inline">

<input type="radio" name="sex" value="option2"> 女性

</label>

<label class="radio-inline">

<input type="radio" name="sex" value="option3">中性

</label>

</div>

Copy after login

表单控件状态

1、焦点状态:

焦点状态是通过伪类:focus来实现的,bootstrap表单控件中的焦点状态删除了outline的默认样式,重新添加阴影效果,下面是

css源码:

1

2

3

4

5

6

.form-control:focus {

border-color: #66afe9;

outline: 0;

-webkit-box-shadow: inset 0 1px 1pxrgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);

box-shadow: inset 0 1px 1pxrgba(0,0,0,.075), 0 0 8px rgba(102, 175, 233, .6);

}

Copy after login

从源码中可以看出,要让控件在焦点状态下有上面的样式效果需要给控件添加类名.form-control

1

2

3

4

5

6

7

8

9

10

<form class="form-horizontal">

<div class="form-group ">

<div class="col-xs-6">

<input type="text" class=" input-lg" placeholder="不是在焦点状态下的效果">

</div>

<div class="col-xs-6">

<input type="text" class="form-control input-lg" placeholder="在焦点状态下的效果">

</div>

</div>

</form>

Copy after login

file、radio、checkbox控件在焦点状态下的效果也与普通的input控件不太一样,下面是源码

1

2

3

4

5

6

7

input[type="file"]:focus,

input[type="radio"]:focus,

input[type="checkbox"]:focus {

outline: thin dotted;

outline: 5px auto -webkit-focus-ring-color;

outline-offset: -2px;

}

Copy after login

2、禁用状态:

在相应得表单控件上添加属性disabled即可,下面是css源码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

.form-control[disabled],

.form-control[readonly],

fieldset[disabled] .form-control {

cursor: not-allowed;

background-color: #eee;

opacity: 1;

}

input[type="radio"][disabled],

input[type="checkbox"][disabled],

.radio[disabled],

.radio-inline[disabled],

.checkbox[disabled],

.checkbox-inline[disabled],

fieldset[disabled] input[type="radio"],

fieldset[disabled] input[type="checkbox"],

fieldset[disabled] .radio,

fieldset[disabled] .radio-inline,

fieldset[disabled] .checkbox,

fieldset[disabled] .checkbox-inline {

cursor: not-allowed;

}

Copy after login

例子:

1

<input type="text" class="form-control" placeholder="表单已禁用" disabled>

Copy after login

如果fieldset设置了disabled属性,整个域都会处于被禁用状态

例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<form role="form">

<fieldset disabled>

<div class="form-group">

<label> 输入框已禁用</label>

<input type="text" class="form-control" placeholder="禁止输入内容">

</div>

<div class="form-group">

<label>下拉框已禁用</label>

<select class="form-control">

<option>1</option>

<option>2</option>

<option>3</option>

<option>4</option>

</select>

</div>

<div class="checkbox">

<label >

<input type="checkbox">选项框被禁用了

</label>

</div>

<button type="submit" class="btn btn-primary">提交</button>

</fieldset>

</form>

Copy after login

效果如下:(鼠标移上去的时候出现禁用的图标,这里是直接截的图看不到这个效果)

3、验证状态

bootstrap提供下面这几种效果:

1、.has-warning:警告状态 黄色

2、 .has-error :错误状态 红色

3、 .has-success:成功状态 绿色

使用的时候只需在form-group容器上对应添加状态类名,三种状态下效果都是一样的,只是颜色不一样而已

例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<form>

<div class="form-group has-success">

<label>成功状态</label>

<input type="text" class="form-control" placeholder="成功状态">

</div>

<div class="form-group has-error">

<label>错误状态</label>

<input type="text" class="form-control" placeholder="错误状态">

</div>

<div class="form-group has-warning">

<label>警告状态</label>

<input type="text" class="form-control" placeholder="警告状态">

</div>

</form>

Copy after login

效果如下:

有时候,在表单验证的时不同的状态会提供不同的icon,如果要在对应的状态下显示icon出来,只需要在对应的状态下添加类名.has-feedback ,注意它要和.has-error,.has-success,.has-warning一起使用。

bootstrap的小图标都是使用@font-face来制作的。如:

1

<span class=”glyphicon glyphicon-warning form-control-feedback”></span>

Copy after login

例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<form>

<div class="form-group has-success has-feedback">

<label> 成功状态</label>

<input type="text" class="form-control" placeholder="成功状态">

<span class="glyphicon glyphicon-ok form-control-feedback"></span>

</div>

<div class="form-group has-error has-feedback">

<label>错误状态</label>

<input type="text" class="form-control" placeholder="错误状态">

<span class="glyphicon glyphicon-remove form-control-feedback"></span>

</div>

<div class="form-group has-warning has-feedback">

<label>警告状态</label>

<input type="text" class="form-control" placeholder="警告状态">

<span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>

</div>

</form>

Copy after login

效果如下:

表单提示信息

一般在制作表单验证时,需要提供不同的提示信息,在bootstrap框架中使用.help-block,将提示信息以块状显示,并且显示在控件底部

下面是css源码:

1

2

3

4

5

6

.help-block {

display: block;

margin-top: 5px;

margin-bottom: 10px;

color: #737373;

}

Copy after login

例子:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<form>

<div class="form-group has-success has-feedback">

<label>成功状态</label>

<input type="text" class="form-control" placeholder="成功状态">

<span class="help-block">输入的信息正确</span>

<span class="glyphicon glyphicon-ok form-control-feedback"></span>

</div>

<div class="form-group has-error has-feedback">

<label>错误状态</label>

<input type="text" class="form-control" placeholder="错误状态">

<span class="help-block">输入的信息有误</span>

<span class="glyphicon glyphicon-remove form-control-feedback"></span>

</div>

<div class="form-group has-warning has-feedback">

<label>警告状态</label>

<input type="text" class="form-control" placeholder="警告状态">

<span class="help-block">请输入正确的信息</span>

<span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>

</div>

</form>

Copy after login

效果如下:


如果不想为bootstrap.css增加自己的代码,而且设计又有这种需要,可以借助bootstrap的网格系统,例如:

1

2

3

4

5

6

7

8

9

10

11

<form role="form">

<div class="form-group">

<label class="control-label" for="inputSuccess1">成功状态</label>

<div class="row">

<div class="col-xs-6">

<input type="text" class="form-control" id="inputSuccess1" placeholder="成功状态" >

</div>

<span class="col-xs-6 help-block">你输入的信息是正确的</span>

</div>

</div>

</form>

Copy after login

以上所述是小编给大家介绍的Bootstrap表单组件的相关内容,希望对大家有所帮助!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

See all articles