问题
I have a form like this on my page:
<form data-bind="submit: AddFolder"></form>
If I have this code in my JS script (of course I've removed all of the unrelated code and tested to make sure I can still recreate with only this code, normally you'll have more code inside here like .get and .post functions):
Sammy(function() {
}).run();
When the form is submitted, the page redirects to a weird URL like ?ko_unique=1
If I remove the Sammy part from my script, this doesn't happen. I think it has something to do with event bubbling and Sammy and Knockout both hooking the onSubmit, and the browser only respecting the return value from the last function called.
回答1:
After much searching, and not finding any answers here on SO, I ended up finding this:
https://groups.google.com/forum/?fromgroups#!topic/sammyjs/EYW-2Ygk3z8
And modified my code to this:
Sammy(function() {
// Override this function so that Sammy doesn't mess with forms
this._checkFormSubmission = function(form) {
return (false);
};
}).run();
So that Sammy never attempts to do anything special when a form is submitted on my page. Since I'm using Knockout, I don't plan on using Sammy for any forms. If you want more complex code or a plugin version you can see the above URL, but for me, and I suspect for most using KO, it's less code and easier to just disable this Sammy feature.
来源:https://stackoverflow.com/questions/14861461/weird-redirect-using-data-bind-submit-sammy-js-and-knockout-js-together