简体   繁体   English

自动完成javascript后,在文本框中自动设置值

[英]Set automatically value in a textbox after autocomplete javascript

I have 4 textbox that are connected each other in my database, the values are: Name surname, team, society, country. 我的数据库中有4个相互连接的文本框,值是:姓,队,社会,国家。

I have already created an autocomplete method for all the values; 我已经为所有值创建了自动完成方法; infact if i try to write a name/society ecc, the method search in the database if the values exist and recommend values. 实际上,如果我尝试编写名称/社团ecc,则该方法会在数据库中搜索值(如果存在)并推荐值。 Another thing that i have already implemented is that if I select a name surname and a team (or other 2 values) it suggests you society and country or what you did not select that are collegated with the other values. 我已经实现的另一件事是,如果我选择一个姓氏和一个团队(或其他2个值),它会提示您社会和国家,或者您未选择的内容与其他值结合使用。

What do i have to do to set automatically the values, without using the select of the user(what i have to do to set automaticcaly the textbox value of the result of the query)? 在不使用用户选择的情况下,我必须怎么做才能自动设置值(我要做什么才能自动设置查询结果的文本框值)? Thank you. 谢谢。 I am using Asp.net mvc and javascript. 我正在使用Asp.net mvc和javascript。

Edit: this the code. 编辑:这是代码。 Html: HTML:

@{
    string autocompletePrefix = "address";
}
            <div class="form-group">
                @Html.CustomLabelFor(model => model.PlayerId, new { @class = "control-label col-md-2" })
                <div class="col-md-10" style="display:inline-flex;">
                    <div class="input-group">
                        @Html.TextBox("Player", null, null, new { @id = autocompletePrefix + "PlayerName", @class = "ui-autocomplete-input form-control" })
                        <span class="input-group-btn">
                            <button id="@autocompletePrefix-PlayerSearcher" class="btn btn-default" type="button">
                                <span class="glyphicon glyphicon-list" id="@autocompletePrefix-playerIconList"></span>
                            </button>
                        </span>
                    </div>
                    <div class="ui-autocomplete ui-front btn-group-vertical"></div>
                    @Html.HiddenFor(model => model.PlayerId, new { @id = autocompletePrefix + "PlayerId" })
                    @Html.ValidationMessageFor(model => model.PlayerId)
                </div>
            </div>


<script type="text/javascript">
        $(document).on('ready readyAgain', function () {
            address = AutocompleteTeamModule('@autocompletePrefix', '@Url.Action("GetHintsForPlayers", "Addresses", new { })', '@Url.Action("GetHintsForTeam", "Addresses", new { })', '@Url.Action("GetHintsForSocieties", "Addresses", new { })', '@Url.Action("GetHintsForCountry", "Addresses", new { })');
        initMultipleSelection();
    });

    </script>

Javascript: Javascript:

function AutocompleteTeamModule( _prefixHtmlId, _hintsForPlayer,_hintsForTeam,_hintsForSociety,_hintsForCountry)
{
    init: function () {
    $("#" + myObj.prefixHtmlId + "-PlayerSearcher").click(function () {
                myObj.PlayerSearcherClick = true;
                $('#' + myObj.prefixHtmlId + 'PlayerName').autocomplete("option", "minLength", 0);
                var value = $("#" + myObj.prefixHtmlId + "PlayerId").val();
                if( value != "")
                    $('#' + myObj.prefixHtmlId + 'PlayerName').autocomplete("search", $("#" + myObj.prefixHtmlId + "PlayerId").val());
                else
                    $('#' + myObj.prefixHtmlId + 'CountryName').autocomplete("search", "");
                $('#' + myObj.prefixHtmlId + 'PlayerName').trigger("focus");
                $("#" + myObj.prefixHtmlId + "-PlayerLoader").toggleClass("hide", false);
                $("#" + myObj.prefixHtmlId + "-PlayerIconList").toggleClass("hide", true);
                $("#" + myObj.prefixHtmlId + "-PlayerCountry").toggleClass("hide", false);
                $('#' + myObj.prefixHtmlId + 'PlayerName').autocomplete("option", "minLength", 2);
            });

              $("#" + myObj.prefixHtmlId + "PlayerName").autocomplete({
                minLength: 2, autoFocus: true, source: function (term, resp) {
                    $.ajax({
                        type: "GET",
                        url: myObj.hintsPlayerUrl,
                        data: { "id": term.term + "-" + $("#" + myObj.prefixHtmlId + "PlayerId").val() },
                        success: function (data) {
                            $("#" + myObj.prefixHtmlId + "-PlayerLoader").toggleClass("hide", true);
                            $("#" + myObj.prefixHtmlId + "-PlayerIconList").toggleClass("hide", false);
                            myObj.PlayerNameData = data.slice();

                            resp(data, function (data) {
                                return data;
                            });
                        },
                        error: function (xhr, txt, err) {
                            iride_error_handler(xhr, txt, err);
                        }
                    })
                },
                select: function (e, data) {
                  var pa=$("#" + myObj.prefixHtmlId + "PlayerId").val();
                    $("#" + myObj.prefixHtmlId + "PlayerId").val(data.item.value);
                    $("#" + myObj.prefixHtmlId + "-PlayerLoader").toggleClass("hide", true);
                    $("#" + myObj.prefixHtmlId + "-PlayerIconList").toggleClass("hide", false);
                    pa = data.item.value;
                    e.preventDefault();

                    var teamisSet = $("#" + myObj.prefixHtmlId + "TeamId").val();
                    if (teamisSet == "")
                        $('#' + myObj.prefixHtmlId + 'TeamName').autocomplete("search", "-" + pa);
                    e.preventDefault();

                    var societyisSet = $("#" + myObj.prefixHtmlId + "SocietyId").val();
                    if (provinceisSet == "")
                        $('#' + myObj.prefixHtmlId + 'ProvinceName').autocomplete("search", "-" + pa);
                    e.preventDefault();

                    var countryisSet = $("#" + myObj.prefixHtmlId + "CountryId").val();
                    if(countryisSet == "")
                    $('#' + myObj.prefixHtmlId + 'CountryName').autocomplete("search", "-" + $("#" + myObj.prefixHtmlId + "CountryId").val());
                    e.preventDefault();
                },
                focus: function (event, ui) {
                    if (ui.item.value != " ") {
                        $(this).val(ui.item.label);
                    }
                    event.preventDefault();
                }
            });
}

You might provide some code samples or be more specific in what components you are using. 您可能会提供一些代码示例,或者更具体地说明正在使用的组件。

Maybe you can use just jQuery (javascript): 也许您可以只使用jQuery(javascript):

$('#textboxID').val('new value of textbox');

Elaborating on the JQuery suggestion- 详细介绍JQuery建议-

I'd definitely like to see some code but based on what I think your code looks like, 我绝对希望看到一些代码,但是基于我认为您的代码的外观,

If you're talking about changing the value of selectors based on user input on other selectors, you can use a switch or if else inside your click handler. 如果你在谈论改变基于其他选择用户输入选择的值,你可以使用一个switch或者if else的点击处理程序中。 If this is a very data-heavy process there's sure to be a more efficient way to do it but if this is small enough it would go something like this- 如果这是一个非常繁重的数据处理过程,那么肯定会有一种更高效的方法来进行处理,但是如果它足够小,则会出现类似以下情况:

    $('submitbutton').click(function() {
       if ($('firstselector').val() == 'certain value') {
          $('secondselector').val("associated value 1");
          $('thirdselector').val("associated value 2");
       } else if ($('firstselector').val() == 'another certain value') {
          // so on and so forth
       };
    });

a switch, if too many options available- to clean up the code 一个开关(如果有太多可用选项)以清理代码

    $('submitbutton').click(function() {
       switch ($('firstselector').val()) {
          case: 'certain value'
             $('secondselector').val("associated value 1");
             $('thirdselector').val("associated value 2");
             break; 
          case: 'another certain value'
             // so on and so forth
             break;
       };
    });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM