从jquery组合框自动填充文本框

时间:2015-11-18 11:59:36

标签: javascript c# jquery asp.net-mvc combobox

在用户输入大于7个字符的代码后,我一直在使用在线教程自动填充包含数据库值的文本框。我已经完成了我想要实现的大部分内容但是我似乎无法从组合框中选择一个值来自动完成文本框。
我没有太多的javascript经验,但我希望问题是我已经拥有的东西很小,有人可以推荐我需要做的更改从组合框中选择值。

public ActionResult MultiColumnComboBox(string SearchFor, string ControlId)
    {
        ViewBag.ProcId = SearchFor.Trim();
        ViewBag.ControlBlockId = "block" + ControlId.Trim();
        ViewBag.ControlId = ControlId.Trim();
        ViewBag.ControlTxtId = "txt" + ControlId.Trim();

        return View();
    }

    public JsonResult LoadComboData(string strSearch, string SearchFor)
    {
        efacsdbEntities db = new efacsdbEntities();
        strSearch = strSearch.Trim();
        if (SearchFor.Trim() == "employee" && strSearch.Length>7)
        {
            var res = (from E in db.allpartmasters
                       where E.partnum.ToLower().Contains(strSearch.ToLower()) || E.partdesc.ToLower().Contains(strSearch.ToLower())
                       select new
                       {
                           E.partnum,
                           E.partdesc
                       }).ToList();
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        return Json(null, JsonRequestBehavior.AllowGet);
    }

<input type="hidden" id="@ViewBag.ProcId" name="@ViewBag.ProcId" value="" />
<input type="hidden" id="@ViewBag.ControlId" name="@ViewBag.ControlId" value="" />
<input type="text" name="@ViewBag.ControlTxtId" id="@ViewBag.ControlTxtId" autocomplete="on" />
<div class="@ViewBag.ControlTxtId renderpart">
    <div class="DataBlock">
        <div id="@ViewBag.ControlBlockId" style="max-width: 520px;">
        </div>
    </div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="../../Scripts/json.debug.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".renderpart").hide();
        var txtid = "#" + '@ViewBag.ControlTxtId';
        var renderpart = "." + '@ViewBag.ControlTxtId';
        var selectlinkvalueid = ".Get" + '@ViewBag.ProcId';

        $(selectlinkvalueid).on("click", function () {
            var value = $(this).attr('id');
            var valueText = $(this).attr('title');
            $("#@ViewBag.ControlId").val(value);
            $(txtid).val(valueText);
            $(renderpart).slideUp("slow");
        });

        $(txtid).keyup(function () {
            var value = $(txtid).val();
            var Procvalue = '@ViewBag.ProcId';
            var controlid = "#" + '@ViewBag.ControlBlockId';

            value = encodeURI(value);
            if (value.length > 2) {
                $.ajaxSetup({ cache: false });
                $.getJSON("/Test/LoadComboData", { strSearch: " " + value, SearchFor: " " + Procvalue }, function (data) {
                    $(controlid).html("");
                    var activecols = $("#hdnActiveColumns").val();
                    var htmlrow = "";
                    var tempprocId = '@ViewBag.ProcId';
                    var jsondata = JSON.stringify(data);
                    $(controlid).html(CreateDynamicTable(jsondata, tempprocId));
                    $(renderpart).slideDown("slow");
                });
                $.ajaxSetup({ cache: true });
            }
            else {
                $(renderpart).slideUp("slow");
            }
        });
        $(txtid).focusin(function () {
            var txtid = "#" + '@ViewBag.ControlTxtId';
            var value = $(txtid).val();
            var Procvalue = '@ViewBag.ProcId';
            var controlid = "#" + '@ViewBag.ControlBlockId';
            value = encodeURI(value);
            if (value.length > 2) {
                $.ajaxSetup({ cache: false });
                $.getJSON("/Test/LoadComboData", { strSearch: " " + value, SearchFor: " " + Procvalue }, function (data) {
                    $(controlid).html("");
                    var htmlrow = "";
                    var tempprocId = '@ViewBag.ProcId';
                    var jsondata = JSON.stringify(data);
                    $(controlid).html(CreateDynamicTable(jsondata, tempprocId));
                    $(renderpart).slideDown("slow");
                });
                $.ajaxSetup({ cache: true });
            }
            else {
                $(renderpart).slideUp("slow");
            }
        });
        function CreateDynamicTable(objArray, tempprocId) {
            var array = JSON.parse(objArray);
            var str = '<table style="width:100%;">';
            str += '<tr>';
            for (var index in array[0]) {
                str += '<th scope="col">' + index + '</th>';
            }
            str += '</tr>';
            str += '<tbody>';
            var flag = false;
            var ids;
            for (var i = 0; i < array.length; i++) {
                str += (i % 2 == 0) ? '<tr>' : '<tr>';
                for (var index in array[i]) {
                    if (flag == false) {
                        ids = array[i][index];
                        flag = true;
                    }
                    str += '<td><a id="' + ids + '" class="Get' + tempprocId + '" title="' + array[i][index] + '" href="#">' + array[i][index] + '</a></td>';
                }
                str += '</tr>';
            }
            str += '</tbody>';
            str += '</table>';
            return str;
        }
    });
    $(document).click(function (evt) {
        var renderpart = "." + '@ViewBag.ControlTxtId';
        var theElem = (evt.srcElement) ? evt.srcElement : evt.target;

        if (theElem.id == "main" || theElem.id == "sub1") {
            $(renderpart).slideUp("fast");
        }
    });
</script>

以下是我正在关注的教程的链接。
Create Multiple column autocomplete combobox

0 个答案:

没有答案