jquery mobile and knockout form submit binding

后端 未结 2 1549
庸人自扰
庸人自扰 2020-12-17 04:47

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

Consider the following markup:

         


        
相关标签:
2条回答
  • 2020-12-17 05:26

    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;
        });
      }
    };
    

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

    <form data-bind="jqmsubmit: myKoSubmitAction">
      <!-- form fields here -->
    </form>
    
    0 讨论(0)
  • 2020-12-17 05:28

    You can also add data-ajax="false" to the <form> element.

    See Submitting Forms.

    0 讨论(0)
提交回复
热议问题