简体   繁体   English

MVC和JQuery Mobile:Radiobutton释放发布状态

[英]MVC and JQuery Mobile : Radiobutton looses state on post

I am using Jquery Mobile on a page which is set data-ajax="false" to always refresh content on load. 我在将data-ajax =“ false”设置为总是刷新加载内容的页面上使用Jquery Mobile。

A javascript set's the radiobutton state (checked/not checked) on document.ready(), however it only is successful on the GET request. javascript设置了document.ready()上的单选按钮状态(选中/未选中),但是仅在GET请求上成功。

When I click the save button and the page loads again after it's POST, then the selection is gone. 当我单击“保存”按钮并且页面在POST之后再次加载时,选择消失了。

    @using (Html.BeginForm())
{
  @Html.HiddenFor(x => x.CurrentCulture);

    <input type="radio" name="radio-language" id="radio-language-eng" value="en-US" />
    <label for="radio-language-eng">English</label>
    <input type="radio" name="radio-language" id="radio-language-esp" value="es-MX" />
    <label for="radio-language-esp">Spanish</label>
    <input type="submit" value="Save Changes" />
}

<script type="text/javascript">
  $(document).ready(function () {
    var currentCulture = $("#CurrentCulture").val();
    $('input[type=radio][name=radio-language]').checkboxradio();
    $("#radio-language-eng").prop("checked", currentCulture == "en-US");
    $("#radio-language-esp").prop("checked", currentCulture == "es-MX");
    $('input[type=radio][name=radio-language]').checkboxradio("refresh");

    $('input[type=radio][name=radio-language]').change(function () {
      $("#radio-language-eng").prop("checked", this.value == "en-US");
      $("#radio-language-esp").prop("checked", this.value == "es-MX");
      $("#CurrentCulture").val(this.value);
    });
  });
</script>

Checking the value of the hiddenfield, it holds the correct culture, so why does the radiobutton looses it's state? 检查hiddenfield的值,它具有正确的区域性,那么为什么单选按钮会失去其状态?

You can replace you code including the script with RadioButtonFor() and strongly bind to your model. 您可以使用RadioButtonFor()替换包含脚本的代码,并牢固地绑定到模型。 Note you need to give the buttons an id attribute to work with the associated label and to prevent invalid html. 请注意,您需要为按钮提供一个id属性,以使用相关的标签并防止html无效。

@Hml.RadioButtonFor(m => m.CurrentCulture, "en-US", new { id = "eng" })
@Html.Label("eng", "English")
@Hml.RadioButtonFor(m => m.CurrentCulture, "es-MX", new { id = "esp" })
@Html.Label("esp", "Spanish")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM