我在我的視圖中有以下代碼。它有兩個同名的單選按鈕。它可以在控制器中正常工作,我可以使用單選按鈕名稱訪問表格集合,該選項使我可以選擇是或否值 因此,如果用戶在表格中選擇「否」,我將訪問它[[rbc]],它給我值「不」在jquery中訪問具有相同名稱的單選按鈕
雖然這不是在jQuery中的情況下,我不能訪問這個單選按鈕來檢查用戶是否選擇了是或否,請建議我如何在jQuery中實現它。
如果必填字段爲「是」,我將爲每個字段分配一個類「驗證」,否則爲空。爲了驗證從jquery控件(客戶端)
當前jQuery代碼迭代所有的單選按鈕。但我需要它迭代一次具有相同名稱的單選按鈕一次。
控制器
signup.Controls = new List<MembershipControls>();
foreach(var controlItem in _membershipcontrols.Table.ToList())
{
signup.Controls.Add(new MembershipControls()
{
Caption = controlItem.Caption,
Content = controlItem.Content,
ControlType = controlItem.ControlType,
GroupTitle = controlItem.GroupTitle,
Mandatory = controlItem.Mandatory == "Yes" ? "validate" : "",
Name = controlItem.Name,
UserId = controlItem.UserId
}); //modify mandatory field to assign class "validate" and empty to not validate
}
signup.Groups = new List<Groups>();
signup.Groups = _groups.Table.ToList();
return View("Signup", signup);
查看
@foreach (var groups in Model.Groups)
{
<label style="font-weight:bold">@groups.GroupTitle</label>
<div style=" border: 1px solid #CCCCCC;padding:5px">
@foreach (var row in Model.Controls.Where(r => r.GroupTitle == groups.GroupTitle))
{
<div style="padding:7px">
@if (row.ControlType == "Single Line Text")
{
<label>@row.Caption</label>
<input type="text" name="@row.Name" class="@row.Mandatory" />
}
else if (row.ControlType == "Multi Line Text")
{
<label>@row.Caption</label>
<textarea name="@row.Name" class="@row.Mandatory"></textarea>
}
else if (row.ControlType == "Yes/No Choice(Radio Buttons)")
{
<div>
<label>@row.Caption</label>
 
<input type="radio" name="@row.Name" value="Yes" class="@row.Mandatory"/>   Yes
 
<input type="radio" name="@row.Name" value="No" class="@row.Mandatory"/>   No
</div>
}
else if (row.ControlType == "Checkbox")
{
<div>
<input type="checkbox" name="@row.Name" class="@row.Mandatory"/> @row.Caption
</div>
}
else if (row.ControlType == "Date")
{
<div>
<label>@row.Caption</label>
<input type="date" name="@row.Name" class="@row.Mandatory"/>
</div>
}
</div>
}
</div>
}
腳本
<script type="text/javascript">
$(document).ready(function() {
$('#signupform').submit(function()
{
$(".validate").each(function() {
if ($(this).is("input[type=radio]")) {
alert($(this).attr('name'));
// here is the logic to check whether user has selcted Yes or No
}
});
});
});
你的單選按鈕沒有驗證類? – 2014-09-24 12:03:50