0

AJAX フォームがあり、ラジオ ボタンの変更イベントで送信したい。

AJAX フォーム:

     @using (Ajax.BeginForm("Vote", "Rate", null ,
                       new AjaxOptions
                           {
                               InsertionMode = InsertionMode.Replace,
                               HttpMethod = "GET",
                               OnFailure = "searchFailed",
                               LoadingElementId = "ajax-loader",
                               UpdateTargetId = "searchresults",
                           },new { id = "voteForm" }))
                {
                <input type="radio" name="Stars" value="1">
                <input type="radio" name="Stars" value="2">
                <input type="radio" name="Stars" value="3">

                }

次のコードを使用していますが、機能しません。

       $("#voteForm").ajaxSubmit({ url: '/Vote/Vote', type: 'get' });
4

2 に答える 2

0

これを試して:

<script type="text/javascript">
    $(function () {
        $("input[name='Stars']").change(function() {
            $("#voteForm").ajaxSubmit({ url: '/Vote/Vote', type: 'get' });
        });
    });
</script>
于 2013-07-15T11:53:58.083 に答える
0

@Babul MirdhaAjax.BeginFormは正常に機能するメカニズムですが、標準とは大きく異なる特定の送信動作をカスタマイズすると、大きな頭痛の種になる可能性があります。今、あなたはそれを知っていると思います。

私 (および他の多くの開発者もそう言うでしょう) は、カスタム動作を開発する必要があるたびに、基本的な Jquery を使用します。このような:

コントローラーで:

public JsonResult Vote(YourModel model)
{
    // do something:
    // model.Stars

    return Json(new { message = "Success" }, JsonRequestBehavior.AllowGet);
}

あなたのモデル:

public class YourModel
{
    // ...
    public int Stars { get; set; }
}

そしてあなたの見解:

<script type="text/javascript">
    $(function () {
        $("#voteForm input[name=Stars]").change(function () {
            $.ajax({
                url: '/Home/Vote',
                type: 'GET',
                data: $("form#voteForm").serialize(),
                dataType: 'json',
                success: function (data) {
                    alert(data.message);
                },
                error: function (jq, message) {
                    alert(message);
                }
            });
        });
    });
</script>

<form id="voteForm" action="@Url.Action("Vote")">
    <input type="radio" name="Stars" value="1" checked="checked"/>
    <input type="radio" name="Stars" value="2" />
    <input type="radio" name="Stars" value="3" />
    <input type="text" name="Tbx" />
</form>

このようにして、動作を完全に制御できます。

于 2013-07-15T19:06:37.417 に答える