Vue.js + jQuery Ajax 进行INSERT和SELECT操作

44 篇文章 0 订阅
10 篇文章 0 订阅

引用Vue.js 和 jQuery.js

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

利用jQuery Ajax INSERT数据到数据库

    <div class="content" id="app">
        <div class="banner">
            <img src="img/title.png" alt="" class="title" />
        </div>
        <ul class="list">
            <li>孩子姓名:</li>
            <li>
                <input type="text" v-model="name" maxlength="5" />
            </li>
            <li>家长手机号:</li>
            <li>
                <input type="text" v-model="phone" maxlength="11" v-on:input="numOnly" />
            </li>
            <li>报名校区:</li>
            <li>
                <select v-model="school">
                    <option v-for="item in schoolList" v-bind:value="item">{{item}}</option>
                </select>
            </li>
            <li>
                <button class="btn" @click="submit">提交</button>
            </li>
        </ul>
    </div>
    <script>
        var vv = new Vue({
            el: "#app",
            data: {
                name: "",
                phone: "",
                school: "上海校区",
                schoolList: [
                    "上海校区",
                    "北京校区",
                    "广州校区",
                    "其他",
                ],
            },
            methods: {
                numOnly: function () {
                    this.phone = this.phone.replace(/[^\d]/g, '');
                },
                submit: function () {
                    if ($.trim(vv.name) == "" || $.trim(vv.phone) == "" || $.trim(vv.school) == "") {
                        alert("请填写完整所有信息!");
                        return;
                    }
                    if (!((vv.phone.substr(0, 1) == "1") && (vv.phone.length == 11))) {
                        alert("请输入正确的手机号码!");
                        return;
                    }
                    $.ajax({
                        type: "get",
                        url: "add.ashx",
                        data: { name: vv.name, phone: vv.phone, school: vv.school },
                        success: function (request) {
                            if (request == "ok") {
                                alert("提交成功,我们会尽快与您联系你!");
                                window.location = window.location.href;
                            }
                            else {
                                alert(request);
                            }
                        }
                    });
                },
            }
        });
    </script>

利用jQuery Ajax SELECT取出数据并加载到页面

    <div id="app">
        <select v-model="school" style="width:100%" @change="bind">
            <option v-for="item in schoolList" v-bind:value="item">{{item}}</option>
        </select>
        <table border="1" width="100%">
            <tr>
                <th width="25%">学生姓名</th>
                <th width="25%">家长电话</th>
                <th width="25%">报名校区</th>
                <th width="25%">提交时间</th>
            </tr>
            <tr v-for="item in students">
                <td>{{item.name}}</td>
                <td>{{item.phone}}</td>
                <td>{{item.school}}</td>
                <td>{{item.CreateDate}}</td>
            </tr>
        </table>
    </div>
    <script>
        var vv = new Vue({
            el: "#app",
            data: {
                school: "全部校区",
                schoolList: [
                    "全部校区",
                    "上海校区",
                    "北京校区",
                    "广州校区",              
                    "其他",
                ],
                students: []
            },
            created: function () {
                this.bind();
            },
            methods: {
                bind: function () {
                    this.students.splice(0, this.students.length);
                    $.ajax({
                        type: "get",
                        dataType: "json",
                        data: { school: this.school },
                        url: "list.ashx",
                        success: function (data) {
                            $.each(data, function (index, item) {
                                var sth = { name: item.name, phone: item.phone, school: item.school, CreateDate: item.CreateDate };
                                vv.students.push(sth);
                            });
                        }
                    });
                }
            }
        });
    </script>

①Vue.js的初始化页面事件:created:function(){  },

<script>
    el: "#app",
    data: {},
    created: function () { this.bind(); },
    methods: {  bind: function () {/*...dosth...*/ }  }
</script>

②清空数组内所有数据:this.arr.splice(0, this.arr.length);

 this.students.splice(0, this.students.length);

③ajax获取到json数据的处理:

                  $.ajax({
                        type: "get",
                        dataType: "json",
                        data: { school: this.school },
                        url: "list.ashx",
                        success: function (data) {
                            $.each(data, function (index, item) {
                                var sth = { name: item.name, phone: item.phone, school: item.school, CreateDate: item.CreateDate };
                                vv.students.push(sth);
                            });
                        }
                    });

 


UPDATE 操作 参考 INSERT操作,前端调用AJAX是一样的,只是后台处理不同 

相关资料:

vue.js :{{模板}}|v-text文本|v-html标签|v-show显示|v-if渲染|v-on事件|v-on修饰|v-for循环|v-bind属性|v-model绑定|AJAX调用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值