2015-02-09 88 views
0

所以,我想改變我的複選框,已選中和未選中狀態的單選按鈕,說是,(選中)或否(未選中)。如何將我的複選框更改爲單選按鈕?

這裏就是我所做的複選框:

筆者認爲:

@Html.CheckBoxUi("PerpendicularCheckbox",@H.GetString("IsPerpendicular"), null, new { style = "margin-right:10px", @class = "type-style-paragraph" }) 

JS:

$('input:checkbox[name=PerpendicularCheckbox]').on({ 
       "change": function() { 
        if (getUpdate()) { 
         var $this = $(this); 
         if (($this).is(':checked')) 
          $("ul li button").click(); 
        } 
       } 
      }); 

      if (!Perpendicular) {     
       $("#PerpendicularCheckbox").prop("checked", false); 
      } 
      else {    
       $("#PerpendicularCheckbox").prop("checked", true); 
      } 

我想知道什麼,我需要將其更改爲單選按鈕,是和沒有選項,在asp.net mvc中使用html擴展?

編輯:

我在單選按鈕loosy嘗試:

@Html.RadioButtonForUi("PerpendicularCheckbox",@H.GetString("IsPerpendicular"), null, new { style = "margin-right:10px", @class = "type-style-paragraph" }) 



$('input:radio[name=PerpendicularCheckbox]').on({ 
        "change": function() { 
         if (getUpdate()) { 
          var $this = $(this); 
          if (($this).is(':checked')) 
           $("ul li button").click(); 
         } 
        } 
       }); 

RadioButtonForUi:

public static MvcHtmlString RadioButtonForUi<TModel, TProperty>(
      this HtmlHelper<TModel> htmlHelper, 
      Expression<Func<TModel, TProperty>> expression, 
      string name, 
      bool IsEnable, 
      bool IsChecked, 
      object onchange = null, 
      string className = "", 
      bool isRequreid = true 

     ) {etc.....} 
+0

使用它你的意思是這樣的嗎? https://msdn.microsoft.com/en-us/library/dd493075(v=vs.100).aspx – 2015-02-09 18:18:38

+0

是的,但我只是想知道如何使用@ html.radiobutton選擇是和不是爲上面的複選框。 – 2015-02-09 18:21:09

+0

爲什麼不使用@ Html.RadioButtonFor如果你需要單選按鈕?! – oqx 2015-02-09 18:22:00

回答

0

這裏是一個測試樣品:

 <div class="form-group"> 
     @Html.LabelFor(model => model.SaleOfPropertyPurchase, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <div class="checkbox"> 
       @Html.RadioButtonFor(model => model.SaleOfPropertyPurchase, true, new { id = "SaleOfPropertyPurchase-true" }) Yes 
       @Html.RadioButtonFor(model => model.SaleOfPropertyPurchase, false, new { id = "SaleOfPropertyPurchase-false" }) No 
       @Html.ValidationMessageFor(model => model.SaleOfPropertyPurchase, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    </div> 

下面是一些示例jquery的反應的單選按鈕點擊,以及建立在表格上最初顯示:

@Scripts.Render("~/bundles/jquery") 
<script type="text/javascript"> 
    $(function() { 
     $('#CurrentPropertyOwner-true').on('change', function() { 
      $('#CurrentProperty').show(); 
     }); 
    }); 
    $(function() { 
     $('#CurrentPropertyOwner-false').on('change', function() { 
      $('#CurrentProperty').hide(); 
     }); 
    }); 

    $(document).ready(function() { 
     var ischecked = $('#CurrentPropertyOwner-true').is(':checked') 
     if (ischecked == true) { 
      $('#CurrentProperty').show(); 
     } 
     var ischecked = $('#CurrentPropertyOwner-false').is(':checked') 
     if (ischecked == true) { 
      $('#CurrentProperty').hide(); 
     } 
    }); 
</script> 
0

你需要使兩個單選按鈕的屬性,一個與「真」的價值和其他與價值「假」,所以選擇的值可以綁定到一個boolean

您自定義HTML助手將需要

namespace YourAssembly.Html 
{ 
    public static class MyHelpers 
    { 
    public static MvcHtmlString BooleanButtonsFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, bool>> expression) 
    { 
     ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 
     string name = ExpressionHelper.GetExpressionText(expression); 
     StringBuilder html = new StringBuilder(); 
     // Yes button 
     string id = string.Format("{0}-yes", name); 
     html.Append(helper.RadioButtonFor(expression, "True", new { id = id })); 
     html.Append(helper.Label(id, "Yes")); 
     // No button 
     id = string.Format("{0}-no", name); 
     html.Append(helper.RadioButtonFor(expression, "False", new { id = id })); 
     html.Append(helper.Label(id, "No")); 
     // enclode in a div for easier styling with css 
     TagBuilder div = new TagBuilder("div"); 
     div.AddCssClass("radiobuttongroup"); 
     div.InnerHtml = html.ToString(); 
     return MvcHtmlString.Create(div.ToString()); 
    } 
    } 
} 

然後添加一個引用的web.config

<namespaces>
<add namespace="YourAssembly.Html "/> 

,並在視圖

@Html.BooleanButtonsFor(m => m.YourBoolProperty) 
相關問題