繁体   English   中英

在CSHTML Razor .NET中将单选按钮设置为默认选中的选项

[英]Setting radio button default selected option in CSHTML Razor .NET

我有这样的HTML标记:

<input type="radio" name="shipping" value="Worldwide"> All Locations<br>
<input type="radio" name="shipping" value="US" checked> US Only<br>
<input type="radio" name="shipping" value="GB"> UK Only


<input type="radio" name="type" value="all"> All Listings
<input type="radio" name="type" value="FixedPrice" checked> Fixed Price
<input type="radio" name="type" value="Auction"> Auction




   <input type="radio" name="condition" value="New" checked> Brand New<br>
                <input type="radio" name="condition" value="Used"> Used<br>

基本上,我要在此处执行的操作是保存用户首选项,然后将其从我的数据库加载到这些复选框中,如下所示:

 @if (ViewBag.UserPreferences!=null)
        {

        @:$("input[name='shipping'][value='" + @ViewBag.UserPreferences.ShippingLocation + "']").attr('checked', 'checked');
        @:$("input[name='type'][value='" + @ViewBag.UserPreferences.ListingType + "']").attr('checked', 'checked');
        @:$("input[name='condition'][value='" + @ViewBag.UserPreferences.Condition + "']").attr('checked', 'checked');


        }

ViewBag.UserPreferences的内容与您在其中任一标签的value属性中看到的值完全相等。

我在这里做错了什么???

您无需逃脱单引号即可添加剃须刀值。 尝试这个:

@:$("input[name='shipping'][value='@ViewBag.UserPreferences.ShippingLocation']").attr('checked', 'checked');

我也很幸运<text>

<text>$("input[name='shipping'][value='@ViewBag.UserPreferences.ShippingLocation']").attr('checked', 'checked');</text>

暂无
暂无

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

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