我有这样的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属性中看到的值。
我在这里做错了什么???
答案 0 :(得分:1)
您不需要从单引号中删除以添加剃刀值。试试这个:
@:$("input[name='shipping'][value='@ViewBag.UserPreferences.ShippingLocation']").attr('checked', 'checked');
我也很幸运<text>
:
<text>$("input[name='shipping'][value='@ViewBag.UserPreferences.ShippingLocation']").attr('checked', 'checked');</text>