JQuery的新功能和我有以下功能。我试图修改它,所以它做了2件事:当我点击其他单选按钮时,不会触发该功能(我认为'#UpdateType input [type =“radio”]'会这样做),并且也会在加载时运行该函数如点击。 #UpdateType是我正在使用它的单选按钮的ID。有没有人有什么建议?JQuery函数 - 在点击和页面加载时运行特定的单选按钮
$(document).ready(updateTypeFunc);
$(document).ready(function() {
$('#UpdateTypeRB input[type="radio"]').click(updateTypeFunc);
});
function updateTypeFunc()
{
if ($(this).attr('id') == 'UpdateType' && $(this).val() == 'Current') {
$('.current-employee').show();
}
else {
$('.current-employee').hide();
}
}
截至目前,当我点击任何单选按钮(无论ID如何)时都会触发。
编辑:HTML
<div class="col-md-10" id="UpdateTypeRB">
<label class="radio-inline">
<input class="btn btn-primary" data-val="true" data-val-required="Please select whether the employee is new or current." id="UpdateType" name="UpdateType" type="radio" value="New"> New
</label>
<br>
<label class="radio-inline">
<input checked="checked" class="btn btn-secondary" id="UpdateType" name="UpdateType" type="radio" value="Current"> Current
</label>
<br>
<span class="field-validation-valid text-danger" data-valmsg-for="UpdateType" data-valmsg-replace="true"></span>
</div>
剃刀:
<div class="form-group">
@Html.LabelFor(m => m.UpdateType, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<label class="radio-inline">
@Html.RadioButtonFor(m => m.UpdateType, "New", new { @class = "btn btn-primary" }) New
</label>
<br />
<label class="radio-inline">
@Html.RadioButtonFor(m => m.UpdateType, "Current", new { @class = "btn btn-secondary" }) Current
</label>
<br />
@Html.ValidationMessageFor(m => m.UpdateType, "", new { @class = "text-danger" })
</div>
</div>
编辑2:更新JQuery的感谢@KristianBarrett。除了我试图实现的$(document).ready以外的其他作品。
我总体努力实现的是隐藏/显示基于此单选按钮打勾的页面上的某些必需元素。它最初加载没有检查,所以用户被迫选择一个。然后,我使用Foolproof来根据这些复选框('Current'tick)执行RequiredIf,并且想要在用JQuery勾选'Current'之后向用户显示它们。问题是验证在提交后抛出一个ErrorMessage时,当前复选框被打勾,但是这些字段被隐藏,直到我再次手动点击它。这些字段默认为隐藏。
你的HTML看起来像什么? '$('#UpdateType input [type =“radio”]')'表示运行这个处理器,处理ID为“UpdateType”的容器中的任何无线电。 – tymeJV
被编辑。那么这是否意味着我的Razor语法会因为#UpdateType的两个ID而搞砸了? – justiceorjustus
好吧 - 你不能有两个相同的ID - ** ID必须是唯一的** – tymeJV