入门
监控属性
使用内置绑定
控制文本和外观
绑定逻辑控制
处理表单属性
click
绑定event
绑定submit
绑定enable
绑定disable
绑定value
绑定hasfocus
绑定checked
绑定options
绑定selectedOptions
绑定uniqueName
绑定
解析模板
高级应用
插件
更多信息
"submit" 绑定
目的
submit
绑定在form表单上添加指定的事件句柄以便该form被提交的时候执行定义的JavaScript 函数。只能用在表单 form
元素上。
The submit
binding adds an event handler so that your chosen JavaScript function will be invoked when the associated DOM element is submitted. Typically you will only want to use this on form
elements.
当你使用 submit
绑定的时候, Knockout会阻止form表单默认的submit动作。换句话说,浏览器会执行你定义的绑定函数而不会提交这个form表单到服务器上。可以很好地解释这个,使用 submit
绑定就是为了处理view model的自定义函数的,而不是再使用普通的HTML form表单。 如果你要继续执行默认的HTML form表单操作,你可以在你的 submit
句柄里返回 true
。
When you use the submit
binding on a form, Knockout will prevent the browser’s default submit action for that form. In other words, the browser will call your handler function but will not submit the form to the server. This is a useful default because when you use the submit
binding, it’s normally because you’re using the form as an interface to your view model, not as a regular HTML form. If you do want to let the form submit like a normal HTML form, just return true
from your submit
handler.
例子
<form data-bind="submit: doSomething">
... form contents go here ...
<button type="submit">Submit</button>
</div>
<script type="text/javascript">
var viewModel = {
doSomething : function(formElement) {
// ... now do something
}
};
</script>
这个例子里,KO将把整个form表单元素作为参数传递到你的submit绑定函数里。 你可以忽略不管,但是有些例子里是否有用,参考:ko.postJson
工具。
As illustrated in this example, KO passes the form element as a parameter to your submit handler function. You can ignore that parameter if you want, but for an example of when it’s useful to have a reference to that element, see the docs for the ko.postJson
utility.
为什么不在submit按钮上使用 click
绑定?
在form上,你可以使用 click
绑定代替 submit
绑定。不过 submit
可以处理其它的函数行为,比如在输入框里输入回车回车的时候可以提交表单。
Instead of using submit
on the form, you could use click
on the submit button. However, submit
has the advantage that it also captures alternative ways to submit the form, such as pressing the enter key while typing into a text box.
参数
- 主参数
你绑定到 submit
事件上的函数。
The function you want to bind to the element’s submit
event.
你可以声明任何JavaScript函数 – 不一定非要是view model里的函数。你可以声明任意对象上的任何函数,例如: submit: someObject.someFunction
。
You can reference any JavaScript function - it doesn’t have to be a function on your view model. You can reference a function on any object by writing submit: someObject.someFunction
.
View model上的函数在用的时候有一点点特殊,就是不需要引用对象的,直接引用函数本身就行了,比如直接写 submit: doSomething
就可以了,而无需写成: submit: viewModel.doSomething
(尽管是合法的)。
Functions on your view model are slightly special because you can reference them by name, i.e., you can write submit: doSomething
and don’t have to write submit: viewModel.doSomething
(though technically that’s also valid).
其它参数
- 无
备注
关于如果传递更多的参数给submit绑定函数,或者当调用非view model里的函数的时如何控制 this
,请参考 click绑定。所有click绑定相关的备注也都适用于 submit
绑定。
For information about how to pass additional parameters to your submit handler function, or how to control the this
handle when invoking functions that aren’t on your view model, see the notes relating to the click binding. All the notes on that page apply to submit
handlers too.
依赖性
除KO核心类库外,无依赖。
(c) knockoutjs.com