设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

AngularJS提交表单的方式

2014-6-30 14:59| 发布者: joejoe0332| 查看: 50084| 评论: 0|原作者: 0x0bject, GoodLoser, it瞌睡虫|来自: 开源中国社区

摘要: 在AngularJS出现之前,很多开发者就面对了表单提交这一问题。由于提交表单的方式繁杂而不同,很容易令人疯掉……然而现在看来,依然会让人疯掉。

展示表单

让我们创建一个HTML来展示我们的表单

<!-- index.html -->

<!doctype html>
<html>
<head>
    <title>Angular Forms</title>

    <!-- LOAD BOOTSTRAP CSS -->
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">

    <!-- LOAD JQUERY -->
        <!-- when building an angular app, you generally DO NOT want to use jquery -->
        <!-- we are breaking this rule here because jQuery's $.param will help us send data to our PHP script so that PHP can recognize it -->
        <!-- this is jQuery's only use. avoid it in Angular apps and if anyone has tips on how to send data to a PHP script w/o jQuery, please state it in the comments -->
       <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

    <!-- PROCESS FORM WITH AJAX (OLD) -->
    <script>
        <!-- WE WILL PROCESS OUR FORM HERE -->
    </script>
</head>
<body>
<div class="container">
<div class="col-md-6 col-md-offset-3">

    <!-- PAGE TITLE -->
    <div class="page-header">
        <h1><span class="glyphicon glyphicon-tower"></span> Submitting Forms with Angular</h1>
    </div>

    <!-- SHOW ERROR/SUCCESS MESSAGES -->
    <div id="messages"></div>

    <!-- FORM -->
    <form>
        <!-- NAME -->
        <div id="name-group" class="form-group">
            <label>Name</label>
            <input type="text" name="name" class="form-control" placeholder="Bruce Wayne">
            <span class="help-block"></span>
        </div>

        <!-- SUPERHERO NAME -->
        <div id="superhero-group" class="form-group">
            <label>Superhero Alias</label>
            <input type="text" name="superheroAlias" class="form-control" placeholder="Caped Crusader">
            <span class="help-block"></span>
        </div>

        <!-- SUBMIT BUTTON -->
        <button type="submit" class="btn btn-success btn-lg btn-block">
            <span class="glyphicon glyphicon-flash"></span> Submit!
        </button>
    </form>

</div>
</div>
</body>
</html>

现在,我们有了表单。我们另外还使用了Bootstrap来使表单看起来不是那么丑。使用Bootstrap语法规则,每个input下含有一个spot来展示我们文本的错误信息。

使用jQuery提交表单

现在,让我们来使用jQuery处理表单提交。我会将所有的代码添加到空的<script>标签中

<!-- index.html -->

...

    <!-- PROCESS FORM WITH AJAX (OLD) -->
    <script>
        $(document).ready(function() {

            // process the form
            $('form').submit(function(event) {

                // remove the past errors
                $('#name-group').removeClass('has-error');
                $('#name-group .help-block').empty();
                $('#superhero-group').removeClass('has-error');
                $('#superhero-group .help-block').empty();

                // remove success messages
                $('#messages').removeClass('alert alert-success').empty();

                // get the form data
                var formData = {
                    'name'                 : $('input[name=name]').val(),
                    'superheroAlias'     : $('input[name=superheroAlias]').val()
                };

                // process the form
                $.ajax({
                    type         : 'POST',
                    url         : 'process.php',
                    data         : formData,
                    dataType     : 'json',
                    success     : function(data) {

                        // log data to the console so we can see
                        console.log(data);

                        // if validation fails
                        // add the error class to show a red input
                        // add the error message to the help block under the input
                        if ( ! data.success) {

                            if (data.errors.name) {
                                $('#name-group').addClass('has-error');
                                $('#name-group .help-block').html(data.errors.name);
                            }

                            if (data.errors.superheroAlias) {
                                $('#superhero-group').addClass('has-error');
                                $('#superhero-group .help-block').html(data.errors.superheroAlias);
                            }

                        } else {

                            // if validation is good add success message
                            $('#messages').addClass('alert alert-success').append('<p>' + data.message + '</p>');
                        }
                    }
                });

                // stop the form from submitting and refreshing
                event.preventDefault();
            });

        });
    </script>

...

这里处理表单有不少的代码。我们有获取表单中变量的代码,有使用AJAX将数据发送至我们的表单的代码,有检查是否有错和显示成功提示的代码。除此之外,我们希望每次表单提交之后,过去的错误信息都会被清除。确实是不少代码。

现在,如果表单中含有错误,则:

如果提交成功:

现在,让我们看使用Angular来提交相同的表单。记住,我们不需要更改任何关于我们的PHP如何处理表单的内容,我们的应用依然会具备相同的功能(在同一个地方展示错误和成功信息)。



酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部