0

我有一个下拉列表。我正在尝试在不使用按钮的情况下在单击事件上保存该下拉列表的数据。我尝试了一些代码,但它不起作用,请帮忙。这是我的下拉列表的视图

    @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);
                }


            });
        });
    });

});

4

1 回答 1

0

您可以像这样调整 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 的方式。您希望在下拉列表更改时提交整个表单吗?

于 2014-07-15T15:32:21.363 回答