随笔 - 165, 文章 - 0, 评论 - 18, 阅读 - 22万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

1.把表单转换出json对象

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//把表单转换出json对象
   $.fn.toJson = function () {
       var self = this,
           json = {},
           push_counters = {},
           patterns = {
               "validate": /^[a-zA-Z][a-zA-Z0-9_]*(?:\[(?:\d*|[a-zA-Z0-9_]+)\])*$/,
               "key": /[a-zA-Z0-9_]+|(?=\[\])/g,
               "push": /^$/,
               "fixed": /^\d+$/,
               "named": /^[a-zA-Z0-9_]+$/
           };
 
       this.build = function (base, key, value) {
           base[key] = value;
           return base;
       };
 
       this.push_counter = function (key) {
           if (push_counters[key] === undefined) {
               push_counters[key] = 0;
           }
           return push_counters[key]++;
       };
 
       $.each($(this).serializeArray(), function () {
           // skip invalid keys
           if (!patterns.validate.test(this.name)) {
               return;
           }
 
           var k,
               keys = this.name.match(patterns.key),
               merge = this.value,
               reverse_key = this.name;
 
           while ((k = keys.pop()) !== undefined) {
               // adjust reverse_key
               reverse_key = reverse_key.replace(new RegExp("\\[" + k + "\\]$"), '');
 
               // push
               if (k.match(patterns.push)) {
                   merge = self.build([], self.push_counter(reverse_key), merge);
               }
 
               // fixed
               else if (k.match(patterns.fixed)) {
                   merge = self.build([], k, merge);
               }
 
               // named
               else if (k.match(patterns.named)) {
                   merge = self.build({}, k, merge);
               }
           }
 
           json = $.extend(true, json, merge);
       });
 
       return json;
   };

 

  

 

 

2.将josn对象赋值给form

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//将josn对象赋值给form
    $.fn.loadData = function (obj) {
        var key, value, tagName, type, arr;
 
        this.reset();
 
        for (var x in obj) {
            if (obj.hasOwnProperty(x)) {
                key = x;
                value = obj[x];
 
                this.find("[name='" + key + "'],[name='" + key + "[]']").each(function () {
                    tagName = $(this)[0].tagName.toUpperCase();
                    type = $(this).attr('type');
                    if (tagName == 'INPUT') {
                        if (type == 'radio') {
                            if ($(this).val() == value) {
                                    $(this).attr('checked', true);
                            }
                        } else if (type == 'checkbox') {
                            arr = value.split(',');
                            for (var i = 0; i < arr.length; i++) {
                                if ($(this).val() == arr[i]) {
                                        $(this).attr('checked', true);
                                    break;
                                }
                            }
                        } else {
                            $(this).val(value);
                        }
                    } else if (tagName == 'SELECT' || tagName == 'TEXTAREA') {
                        $(this).val(value);
                    }
                });
            }
        }
    }

 

2.通过Jquery ajax提交表单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$('#btn_submit').click(function (event) {
 
    if(!$('#form_node').valid())
    {
        return;
    }
 
 
    $(this).html('<i class="ace-icon fa fa-floppy-o  bigger-150"></i>正在提交');
    $(this).attr('disabled', true);//禁用表单提交按钮,避免重复提交
 
 
    // 获取表单数据
    var formData = $('#form_node').serialize();
 
    // 发送Ajax请求
    $.ajax({
        url: 'url',
        type: 'POST',
        data: formData,
        success: function (data) {
            // 处理服务器返回的数据
            console.log(data);
             
            $('#form_node')[0].reset();//清空表单数据
 
           //恢复表单提交按钮功能
            setTimeout(function () {
                $('#btn_submit').attr('disabled', false);
                $('#btn_submit').html('<i class="ace-icon fa fa-floppy-o  bigger-150"></i>保存');
            }, 2000);
 
        },
        error: function (jqXHR, textStatus, errorThrown) {
            // 处理请求失败的情况
            console.error(textStatus, errorThrown);
 
           //恢复表单提交按钮功能
            setTimeout(function () {
                $('#btn_submit').attr('disabled', false);
                $('#btn_submit').html('<i class="ace-icon fa fa-floppy-o  bigger-150"></i>保存');
            }, 2000);
        }
    });
}); 

 

服务端代码:

1
2
3
4
5
6
7
8
9
10
[HttpPost]
public async Task<IActionResult> AddNode(AreaInfo areaInfo)
{
}
 
 
[HttpPost]
public async Task<IActionResult> AddNode(string name,int age,string address......)
{
}

  

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示