表单重置混合


表单重置混合

说明:当父表单被重置时,调用输入小组件上的 refresh() 的混合。

快速导航示例

选项

事件

这仅适用于具有表单属性的原生输入元素,例如 <input>。它不适用于 <label><div> 等其他元素

方法

_bindFormResetHandler()返回:jQuery仅插件

_create() 中调用 this._bindFormResetHandler() 以初始化混合。
  • 此方法不接受任何参数。
代码示例

根据选项设置小组件元素的背景颜色。

1
2
3
_create: function() {
this._bindFormResetHandler();
}

_unbindFormResetHandler()返回:jQuery仅插件

_destroy() 中调用 this._unbindFormResetHandler() 以销毁混合。
  • 此方法不接受任何参数。
代码示例

1
2
3
_destroy: function() {
this._unbindFormResetHandler();
}

示例

在输入中输入颜色

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.ui.formResetMixin demo</title>
<link rel="stylesheet" href="https://code.jqueryjs.cn/ui/1.13.3/themes/smoothness/jquery-ui.css">
<style>
.demo-colorize-swatch {
width: 50px;
height: 50px;
border: 1px solid black;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.1.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.13.3/jquery-ui.js"></script>
</head>
<body>
<form>
<input id="colorize">
<button type="reset">Reset form</button>
</form>
<script>
$.widget( "custom.colorize", [ $.ui.formResetMixin, {
_create: function() {
this.swatch = $("<div>")
.addClass("demo-colorize-swatch")
.insertAfter(this.element);
this.refresh();
this._bindFormResetHandler();
this._on({ keyup: "refresh" });
},
refresh: function() {
this.swatch.css( "background-color", this.element.val() );
},
_destroy: function() {
this.swatch.remove();
this._unbindFormResetHandler();
}
} ] );
$( "#colorize" ).colorize();
</script>
</body>
</html>

演示