jquery mobile 和淘汰赛表单提交绑定 [英] jquery mobile and knockout form submit binding

查看:20
本文介绍了jquery mobile 和淘汰赛表单提交绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表单提交行为方面,我偶然发现了 Knockoutjs 和 jquery mobile 之间明显的不兼容.

I stumbled on an apparent incompatibility between knockoutjs and jquery mobile when it comes to form submit behavior.

考虑以下标记:

<form data-bind="submit: myKoSubmitAction">
   <!-- form fields here -->
</form>

目的是淘汰赛阻止服务器发布/获取,而是调用 myKoSubmitAction.jqm 还将阻止仅针对 jqm 的标准提交行为,原因是表单提交被 ajax 请求替换.

The intention is that knockout prevents server post/get and instead calls myKoSubmitAction. jqm will also prevent standard submit behavior only for jqm the reason is that the form submit is replaced by an ajax request.

因此,虽然敲除(大概)成功阻止了标准服务器请求,但它无法阻止 jqm 发送 ajax 请求.

So while knockout (presumably) succeeds at preventing the standard server request, it fails to prevent jqm from sending an ajax request.

我在一个 google 小组中找到了这个问题的答案,并认为它也应该在 SO 上.见下文

I found the answer to this problem in a google group and thought it should be on SO as well. See below

推荐答案

我能找到的最佳解决方案是以下自定义 ko 绑定:

The best solution I have been able to find is the following custom ko binding:

//This binding fixes apparent incompatibility between knockout and jqm
ko.bindingHandlers.jqmsubmit = {
  init: function (el, accessor, allbindings, vm) {
    ko.bindingHandlers.submit.init(el, accessor, allbindings, vm);
    $(el).submit(function (e) {
        // prevent the submit behavior
        e.preventDefault();
        e.stopPropagation();
        return false;
    });
  }
};

用于代替标准提交ko绑定:

To be used in the place of the standard submit ko binding:

<form data-bind="jqmsubmit: myKoSubmitAction">
  <!-- form fields here -->
</form>

这篇关于jquery mobile 和淘汰赛表单提交绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆