点击保存下拉列表的数据使用Java Script, Jquery或Ajax



我有一个下拉列表。我试图保存该下拉列表的数据单击事件而不使用按钮。我已经尝试了一些代码,但它不工作,请帮助。下面是下拉列表

的视图
    @model MyYello.Admin.Models.FeedBack
@{
ViewBag.Title = "Feed Back";
 }

  @*@using (Ajax.BeginForm("SelectFeedBack", "Admin", new AjaxOptions { HttpMethod = "POST", UpdateTargetId =     "mainContent" }, new { @id = "formId" }))
 *@
 <form method="post" id="formId" action="@Url.Action("SelectFeedBack","Admin")">
@Html.ValidationSummary(true);
<fieldset>
    @Html.HiddenFor(item => item.FeedBackId)
    <legend>Create Notes</legend>
    <div class="editor-label">
        @Html.LabelFor(item => item.FeedBackDrpDown, "Select feed Back")
    </div>
    @Html.DropDownList("FeedBack")

    <input type="hidden" id="isNewNote" name="isNewNote" value="false" />
   @*        <p>
        <input type="Submit" value="Save" id="Save" />
    </p>*@
    @*  @Url.Action("CreateNote", "Admin")*@
</fieldset>
 </form>
<script type="text/javascript">
$(function () {
    $("#FeedBack").change(function () {

        console.log("test");
        $("#formId").submit(function () {
            console.log("test1");
            $.ajax({
                type: "POST",
                //url: urlAction,
                data: {},
                datatype: "JSON",
                contentType: "application/json; charset=utf-8",
                success: function (returndata) {
                    if (returndata.ok)
                        window.location = returndata.newurl;
                    else
                        window.alert(returndata.message);
                }

            });
        });
    });
});

你可以这样调整onChange-Method:

$("#FeedBack").change(function () {
    var urlAction = "/whatever/url/"; // someURL
    // var urlAction = $("#FormId").attr("action"); // or grab the form-url?
    var postData = {
            "whateverName" : $(this).val() // selected drop-down-value
    };
    $.ajax({
        type: "POST",
        url: urlAction,
        data: postData, // send postData-Object
        dataType: "JSON",
        contentType: "application/json; charset=utf-8",
        success: function (returndata) {
            // make shure that the attributes ok,newurl and message are available - otherwise this throws an error and your script breaks
            if (typeof returndata.ok !== "undefined" && typeof returndata.newurl !== "undefined" && returndata.ok)
                window.location.href = returndata.newurl;
            else
                window.alert(returndata.message);
        }
    });
});

这是你提交选择字段值到任意URL的方式。当下拉菜单更改时,您是否希望提交整个表格?

最新更新