我有这样的ASP.NET MVC代码:
@Html.Label("Last Name", htmlAttributes: new { @class = "control-label required-label" }) <br />
@Html.TextBoxFor(model => model.LastName, new { maxlength = 20, @class = "form-control", @autocomplete = "off" })
<span id="spnRCLastName" class="text-danger"></span>
我根据keypress事件的某些条件设置了Javascript验证。我使用myform,因为我们有多个输入元素
$('#myform input').keypress(function (e) {
if ($(this).length > 0)
$(this).closest(".text-danger).empty();
});
但$(this).closest(".text-danger")
未标识<span>
元素。我试过了:
$(this).next("span")
$(this).nextAll("span:first")
但没有运气。如何用类名识别下一个跨度?
答案 0 :(得分:1)
为该组提供父div:
<div>
@Html.Label("Last Name", htmlAttributes: new { @class = "control-label required-label" }) <br />
@Html.TextBoxFor(model => model.LastName, new { maxlength = 20, @class = "form-control", @autocomplete = "off" })
<span id="spnRCLastName" class="text-danger"></span>
</div>
在JS中
$(this).parent().find("span").empty();