【问题标题】:Populate textbox from checkbox list using ASP.NET and AJAX使用 ASP.NET 和 AJAX 从复选框列表中填充文本框
【发布时间】:2010-08-03 05:36:16
【问题描述】:

我的页面上有一个文本框控件和一个复选框列表控件。

我首先在代码隐藏中使用员工对象列表填充我的复选框列表。员工有一个 id 和一个名字。两者都显示在复选框列表中,如下所示:

  • 吉姆 (1)
  • 亚历克斯 (2)
  • 加里 (3)

当用户选中其中一个框时,需要在文本框中填写员工姓名。因此,如果选择了 Jim,则文本框值为“Jim”。它还需要支持多个值,所以如果选择了 Jim 和 Gary,那么文本框的值为“Jim, Gary”。

此外,如果用户在文本框中输入有效值,则应检查正确的员工。这需要支持名称和 ID。因此,如果我在文本框中输入“1,Alex”,然后在文本框外单击,则应该选择 Jim 和 Alex。

我正在使用 ASP.NET,我需要使用 AJAX 来执行此操作,但我没有使用 jQuery 和 AJAX 的经验。谁能给我一个简单的例子来说明如何做到这一点?

【问题讨论】:

    标签: c# asp.net jquery ajax


    【解决方案1】:

    这是我一起完成的一些事情,可能会帮助您入门。它没有经过测试,也没有完成,但我现在没有时间做这一切,所以认为这可能会有所帮助。肯定需要做更多的检查和配对,我留给你实施。

    $(function() {
        // gets all checkbox items by a common class you will put on all of them
        $('.checkboxlistclass').click(function(e) {
            var txt = $(this).text();
            var employeeName = txt; // change this to parse out the name part and remove the id
    
            var currentTxtVal = $('#textboxid').val();
            var newVal = currentTxtVal + ',' + employeeName; // check if you need the comma or not
    
            $('#textboxid').val(newVal);
        });
    
        // textboxid is the id of the textbox
        $('#textboxid').change(function(e) {
            var textboxVal = $(this).val();
    
            // split on the comma and get an array of items
            // this is dummy data
            var items = [];
            items.push('Jim');
            items.push(2);
    
            // go through all the items and check if it's a number or not
            // if it's a number do a selection based on the id in parenthesis
            // if not then check first part for name match
            for (var i=0; i < items.length; i++) {
                if (isNaN(parseInt(items[i])) {
                    $(".checkboxlistclass:contains('" + items[i] + " ('").attr('checked', true);
                }
                else {
                    $(".checkboxlistclass:contains(' (" + items[i] + ")'").attr('checked', true);
                }
            }
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2017-06-03
      • 2015-12-04
      • 1970-01-01
      相关资源
      最近更新 更多