gpt4 book ai didi

mvvm - 在 Knockout 中进行标准 POST 表单验证

转载 作者:行者123 更新时间:2023-12-03 10:29:15 25 4
gpt4 key购买 nike

为了允许在提交之前对表单进行验证,
我想从我定义的 Submit 方法进行简单的 HTTP POST 提交。

View 代码:

   <form data-bind="submit: BaseSubmit" id="FormId">
<!-- Some rows ... -->
<button type="submit">Create</button>
</form>

以及 JS 行为
this.viewModel.BaseSubmit = 
function(formElements) {
//Here the code to make the submit redirection
};

这可能吗 ?
提前谢谢。

最佳答案

成功验证后,确保和 return true从您的绑定(bind)处理程序:

this.viewModel.BaseSubmit = 
function(formElements) {
if (/* validation is successful */) {
return true;
} else {
/* handle validation errors */
}
};
示例: http://jsfiddle.net/CCNtR/22/
来自 the documentation :

By default, Knockout will prevent the event from taking any default action. For example if you use the event binding to capture the keypress event of an input tag, the browser will only call your handler function and will not add the value of the key to the input element’s value. A more common example is using the click binding, which internally uses this binding, where your handler function will be called, but the browser will not navigate to the link’s href. This is a useful default because when you use the click binding, it’s normally because you’re using the link as part of a UI that manipulates your view model, not as a regular hyperlink to another web page.

However, if you do want to let the default action proceed, just return true from your event handler function

关于mvvm - 在 Knockout 中进行标准 POST 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15811386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com