0

ポストバック付きの最高のusag Razorチェックボックスは何ですか? たとえば、次のようなクラスがあります

public class Person
{
    public string Name { get; set; }
    public string SurName { get; set; }
    public bool hi { get; set; }
}

およびビュー(スクリプト付き):

 <script type="text/javascript">
$(function () {
    $('#hi').change(function () {
        $(this).closest("form").submit();
    });
});
</script>
@using (Html.BeginForm(FormMethod.Post))
{
@Html.ValidationSummary(true)

<fieldset>
    <legend>Person</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.SurName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.SurName)
        @Html.ValidationMessageFor(model => model.SurName)
    </div>
    @Html.CheckBoxFor(model => model.hi)

</fieldset>
}

「こんにちは」チェックボックスをクリックしたときにポストバックが必要です。どうすればいいですか?

4

2 に答える 2

2

少しjQuery:

$(function(){
    $('#hi').change(function () {
      $(this).closest("form").submit();     
    });
});
于 2013-04-27T14:34:58.140 に答える
1

私はまだこれを試していません。しかし、私の考えでは、Html.BeginForm(FormMethod.Post) 中かっこ内に送信ボタンを追加し、それにスタイルを追加して display=none にします。つまり、隠されます。次に、jquery を使用して onValueChange などのチェックボックス イベントを追加します (または、おそらく onClick でしょうか?、すべてのイベントを暗記するわけではありません)。イベント ブロック内で、$('#btnInvisibleButton') によるボタン クリックをシミュレートします。クリック(); Safari で .Clic() の実行に問題があったことに注意してください (そのブラウザーに固有のコードを追加することで回避する方法があるため、すべてのブラウザーでコードをテストする必要があります。

次のコードを試してください。送信ボタンが form タグ内にあることを確認してください。

$('#yourCheckBox').change(function () {
  $("#btnInvisibleButton").click();
});
于 2013-04-27T12:57:19.477 に答える