博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery Ajax验证实例
阅读量:4606 次
发布时间:2019-06-09

本文共 2412 字,大约阅读时间需要 8 分钟。

除了jQuery validate 验证外,我们常用到的还有ajax异步验证。其常用的基本格式和jQuery validate 差不多。

需求:

  根据获取到radiobuttons的最新值和输入的cUserId的值来判断记录是否存在并进行相应处理。

 

对应的ajax代码如下:

function checkUser() {            $.ajax({                url: '${ctx}/doctor/doctormsgpushy/find',                type: 'post',                dataType: "json",                data: {                    appType: function () {                        return $('input:radio:checked').val();                    },                    cUserId: function () {                        return $("#cUserId").val();                    }                },                success: function (data) {                    //成功时执行的代码,执行成功不管返回json数据有没有值                },                error: function (json) {                    alert("查找失败");                }            });

  data 表示的是后台方法的返回值。

  ajax异步验证与validate的区别在于它的返回值必须是json数据,如下:

@RequestMapping(value = "find")    public JSON find(String cUserId, String appType) {        DoctorGetuiBind doctorGetuiBind = new DoctorGetuiBind();        doctorGetuiBind.setUserId(cUserId);        if (appType.equals("0")) {            doctorGetuiBind.setDeviceType("3");        } else if (appType.equals("1")) {            doctorGetuiBind.setDeviceType("4");        }        List
doctorGetuiBindList = doctorGetuiBindService.findList(doctorGetuiBind); if (doctorGetuiBindList.size() > 0) { JSONArray json = new JSONArray(); JSONObject js = null; for (int i = 0; i < doctorGetuiBindList.size(); i++) { js = new JSONObject(); js.put("clientId", doctorGetuiBindList.get(i).getClientId()); json.add(js); } String str = json.toString(); return json; } else { return null; } }

  我这里是根据查找结果有可能是多条数据,因此采用了一个JSONArray 对象用来保存多条记录。在jQuery ajax 中的success 方法中的实现如下:

if (data != null && data != "") {                        for (var i = 0; i < data.length; i++) {                            id = data[i].clientId;                            $("#clientIdSelect").append("");                        }                    }

  我这里的操作是根据查询的结果的记录数来添加一个下拉列表的选项。

转载于:https://www.cnblogs.com/youyefly/p/5684371.html

你可能感兴趣的文章
Atitit. Xss 漏洞的原理and应用xss木马
查看>>
MySQL源码 数据结构array
查看>>
(文件过多时)删除目录下全部文件
查看>>
T-SQL函数总结
查看>>
python 序列:列表
查看>>
web移动端
查看>>
pythonchallenge闯关 第13题
查看>>
linux上很方便的上传下载文件工具rz和sz使用介绍
查看>>
React之特点及常见用法
查看>>
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>