jq使用ajax失去焦点

Ajax = 异步 JavaScript 和XML。
Ajax是一种用于创建快速动态网页的技术。
HTML

<script>
    $(function () {

        $("#L_email").blur(function () {
            // 1. 获取文本框中的内容
            // 2. 发送ajax 请求
            var content = $("#L_email").val();
            //console.log(content);
            //alert(content);

            $.ajax({
                url: '/seller/type_add_ajax/',
                type: 'GET',
                data: {'name': content},
                success: function (res) {
                    //alert('成功了');
                    //console.log(res);
                    if (res.status == 'true') {
                        alert('此商品类型已经存在了');
                        $("#error").text('此商品类型已经存在了')

                    } else {
                        $("#error").text('')
                    }
                },
                error: function (res) {
                    alert('error了')
                }
            })

        });

    })

</script>

优化后

<script>
    $(function () {

        $("#btn").click(function () {
            // 1. 获取文本框中的内容
            var content = $("#L_email").val();
            //alert(content);

            // 2. 发送ajax 请求
            $.ajax({
                url: '/seller/type_add1_ajax/',
                type: 'POST',
                data: {'name': content, 'csrfmiddlewaretoken': "{{ csrf_token }}"},
                success: function (res) {

                    if (res.status == 'true') {
                        alert('此商品类型已经存在了');
                        $("#error").text('此商品类型已经存在了')

                    } else {
                        $("#error").text('');
                        //alert('保存成功了...');
                        var bool = confirm('保存成功,是否继续添加类型?');
                        if (bool) {
                            $("#L_email").val(''); // 清空文本框中的内容
                        } else {
                            // window.location.href 在当前页面打开url
                            window.location.href = 'http://127.0.0.1:8999/seller/type_list/';
                        }
                    }
                },
                error: function (res) {
                    alert('error了')
                }
            })
        });
    })

</script>
#  ajax 添加类
def type_add_ajax(request):
    dic = {'status': 'false'}
    if request.method == 'POST':
        # 1. 获取ajax 提交过来的内容
        type_name = request.POST.get('name')
        if type_name:
            # 2. 去数据库中查询
            ret = models.GoodsType.objects.filter(name=type_name)
            if ret:
                """数据库中存在"""
                dic['status'] = 'true'
            else:
                # 3. 返回
                """数据库中不存在"""
                models.GoodsType.objects.create(name=type_name)

    return JsonResponse(dic)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值