[英]Populate textbox from checkbox list using ASP.NET and AJAX
我的页面上有一个文本框控件和一个复选框列表控件。
首先,我在复选框列表中使用隐藏代码中的员工对象列表。 员工有一个ID和一个名字。 两者都显示在复选框列表中,如下所示:
用户选中其中一个框时,需要在文本框中填充员工的姓名。 因此,如果选择了Jim,则文本框值为“ Jim”。 它还需要支持多个值,因此,如果选择Jim和Gary,则文本框值为“ Jim,Gary”。
另外,如果用户在文本框中输入有效值,则应检查正确的员工。 这需要支持名称和ID。 因此,如果我在文本框中输入“ 1,Alex”,然后在文本框外单击,则应该选择Jim和Alex。
我正在使用ASP.NET,并且需要使用AJAX进行此操作,但是我没有使用jQuery和AJAX的经验。 有人可以给我看一个简单的例子吗?
我共同完成的这件事可能有助于您入门。 它没有经过测试并且还没有完成,但是我现在没有时间做所有事情,所以认为这可能会有所帮助。 肯定需要做更多的检查和配对工作,而我留给您实施。
$(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.