我是javascript和jquery的新手。我正在创建一个编辑表单,供用户编辑数据库中的值。我已经填充了一个Html editorFor with JSON
和Jqueryui autocomplete dropdown
供用户更改值,但是我还创建了一个输入文本框,供用户添加新项目到自动完成功能,然后将其设置为自动完成功能的选定选项。
我的问题:
我设法将用户输入添加到the autocomplete dropdown
中并返回用户输入的ID,但是我不确定如何在自动完成中选择相应的项目
可以帮忙吗?谢谢。
这是我的观点:
<div class="col-md-10" id="float">
@Html.HiddenFor(model => model.renovationDetail.ApartmentID)
@Html.EditorFor(model => model.renovationDetail.Apartment.ApartmentNumber, new { htmlAttributes = new { @class = "form-control", placeholder = "Select Apartment Number" } })
@Html.ValidationMessageFor(model => model.renovationDetail.ApartmentID, "", new { @class = "text-danger" })
</div>
<a href="#" id="attach_link">Add new Apartment Number</a>
<div id='viewAddBox'>
<input type="text" id="myText" placeholder="Enter here the new apartment number">
<button id="buttonAddApart">Add</button>
</div>
这是我的javascript:
$("#buttonAddApart").click(function () {
var x = document.getElementById("myText").value;
if (x === "")
{
alert("Please enter a new apartment number");
}
else
{
var url = "/Renovations/AddApartFromRenov";
var apartNumber =x;
var building = $('#renovationDetail_ApartmetBlockID').val();
$.get(url, { apartNum: apartNumber, buildingID: building },
function (data) { } );
这是我的控制者:
public int AddApartFromRenov (string apartNum, string buildingID)
{
int blockID;
Apartment newApart = new Apartment();
if (!string.IsNullOrEmpty(buildingID))
{
blockID = Convert.ToInt32(buildingID);
rn.AddApartment(apartNum, blockID);
newApart = rn.GetOneApartment(blockID, apartNum);
}
return newApart.ApartmentID;
}