简体   繁体   中英

how to get value from alert in Java script in MVC4

I have a form with textbox which uses autocomplete functionality. I need to get the value from javascript to my text box. I have my desired value in an alert message but I can't store that value to my textbox. My code is below.

$(function () {
    $("#Name").autocomplete({
        source: '@Url.Action("GetProducts")',
        select: function (event, ui) {
            alert(ui.item.value);
            return true;
        }
    });
});
<div class=" form-group " style="width:30%">
    <label>Search By Name</label> @Html.TextBox("Name", null, new { id = "Name" })
</div>

Assign ui.item.value to value attribute of textbox. Like this,

<script type="text/javascript">
    $(function () {
        $("#Name").autocomplete({
            source: '@Url.Action("GetProducts")',
            select: function (event, ui) {
               $('#Name').attr('value',ui.item.value);
                return true;
            }
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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