简体   繁体   English

使用ASP.NET和AJAX从复选框列表填充文本框

[英]Populate textbox from checkbox list using ASP.NET and AJAX

I have a textbox control and a checkbox list control on my page. 我的页面上有一个文本框控件和一个复选框列表控件。

I am first populating my checkbox list with a list of employee objects in the code-behind. 首先,我在复选框列表中使用隐藏代码中的员工对象列表。 An employee has an id and a name. 员工有一个ID和一个名字。 Both are displayed in the checkbox list like so: 两者都显示在复选框列表中,如下所示:

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

When a user checks one of the boxes, the employee's name needs to be populated in the textbox. 用户选中其中一个框时,需要在文本框中填充员工的姓名。 So if Jim is selected, the textbox value is "Jim". 因此,如果选择了Jim,则文本框值为“ Jim”。 It also needs to support multiple values, so if Jim and Gary are selected, the textbox value is "Jim, Gary". 它还需要支持多个值,因此,如果选择Jim和Gary,则文本框值为“ Jim,Gary”。

Also, if a user enters a valid value in the textbox, the correct employee should be checked. 另外,如果用户在文本框中输入有效值,则应检查正确的员工。 This needs to support names and id's. 这需要支持名称和ID。 So if I enter "1,Alex" in the textbox and then click outside the textbox, Jim and Alex should be selected. 因此,如果我在文本框中输入“ 1,Alex”,然后在文本框外单击,则应该选择Jim和Alex。

I'm using ASP.NET and I need to do this using AJAX, but I have no experience with using jQuery and AJAX. 我正在使用ASP.NET,并且需要使用AJAX进行此操作,但是我没有使用jQuery和AJAX的经验。 Could someone show me a simple example of how to do this? 有人可以给我看一个简单的例子吗?

Here is something I through together that might help get you started. 我共同完成的这件事可能有助于您入门。 It's not tested and not complete but I don't have time to do it all right now, so thought this might help. 它没有经过测试并且还没有完成,但是我现在没有时间做所有事情,所以认为这可能会有所帮助。 There definitely needs to be a lot more checks and paring done that I left out for you to implement. 肯定需要做更多的检查和配对工作,而我留给您实施。

$(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);
            }
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ASP.NET中的复选框启用文本框? - Enabling a Textbox using a checkbox in ASP.NET? 如何使用json和ajax从asp.net中的sql server数据库填充列表 - how to populate list from sql server database in asp.net using json and ajax 使用 ASP.NET Core MVC 根据从下拉列表中选择的值填充文本框 - Populate a textbox based on selected value from dropdown using ASP.NET Core MVC 使用 ASP.NET MVC 根据从下拉列表中选择的值填充文本框 - Populate a textbox based on selected value from dropdown using ASP.NET MVC 使用 ASP.NET MVC 根据从级联下拉列表中选择的值填充文本框 - Populate a textbox based on selected value from cascading dropdown using ASP.NET MVC 有没有一种优雅的方法可以使用ASP.NET验证器比较复选框和文本框? - Is there an elegant way to compare a checkbox and a textbox using ASP.NET validators? 如何使用CheckBox实现从Asp.Net GridView向TextBox添加和删除收件人 - How to implement add & remove recipients from Asp.Net GridView to TextBox using CheckBox asp.net webservice jquery填充文本框 - asp.net webservice jquery populate textbox 如何填充 <select>在asp.net核心mvc中使用ajax - How to populate <select> using ajax in asp.net core mvc 在 ASP.Net Core 中使用 AJAX 将复选框列表类的值发送到服务器 - To send values of a checkbox list class to the server using AJAX in ASP.Net Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM