如何防止表格提交 [英] How to prevent form submit

查看:62
本文介绍了如何防止表格提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将KnockoutJS与SammyJS一起用于一页应用程序.

I am using KnockoutJS with SammyJS for one page application.

在html中,我具有如下形式的标签

In the html I have form tag as follow

<form data-bind='submit: search'>
  <label>Find user:</label>
  <input data-bind='value: name' />
</form>

在我的viewmodel中,声明了两个函数和sammy路由url

and in my viewmodel, declared two functions and sammy route url

function ViewModel() {
    var self = this;
    self.name = ko.observable("");
    self.search = function () {
      alert(self.name);
    };

    Sammy(function () {
        this.get('#:id', function () {
           //do something....
        });           
    }).run();
}

ko.applyBindings(new ViewModel());

所有代码都运行良好,直到我在文本框中键入内容然后提交表单. 我希望警报窗口后没有URL浏览,但是URL更改为类似"http://localhost:8258/undefined?"的内容. 我原来的网址是"http://localhost:8258"

All code works good, until I type something in textbox then submit the form. I expected no url browsing after alert window, but url is changed to something like this "http://localhost:8258/undefined?" my original url is "http://localhost:8258"

我怀疑sammy的网址路由,因此从javascript代码中删除了sammy的代码,然后警报窗口后url不会更改. 也许我不明白傻瓜的工作原理.

I doubted sammy url routing, so removed sammy code from javascript code, then url does not change after alert window. Maybe I do not understand how sammy works.

在这种情况下如何防止url更改?

How to prevent url change it this case?

推荐答案

Sammy将自身绑定到表单,使您也可以为其注册路由.

Sammy binds itself to forms to enable you to register routes for them as well.

<form action="#1234" method="post">

JS:

Sammy(function() {
    this.post('#:id', function() {
        // do something...

        return false; // avoid form submission
    });

    // ...
}).run();

这篇关于如何防止表格提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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