如何使用jQuery更改剃刀视图,onFocus和onBlur中文本框的颜色

时间:2013-08-19 10:34:09

标签: asp.net-mvc asp.net-mvc-3 jquery razor

如何在onFocus或onBlur上更改textBox(文本框在剃刀视图中)的背景颜色。我不明白使用哪个选择器。

我的剃刀

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>student</legend>

        @Html.HiddenFor(model => model.id)

        <div class="editor-label">
            @Html.LabelFor(model => model.firstname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.firstname)
            @Html.ValidationMessageFor(model => model.firstname)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.lastname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.lastname)
            @Html.ValidationMessageFor(model => model.lastname)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.fathername)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.fathername)
            @Html.ValidationMessageFor(model => model.fathername)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
渲染后的

剃刀

<div class="editor-label">
            <label for="firstname">firstname</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="firstname" name="firstname" type="text" value="test1" />
            <span class="field-validation-valid" data-valmsg-for="firstname" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="lastname">lastname</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" id="lastname" name="lastname" type="text" value="test1" />
            <span class="field-validation-valid" data-valmsg-for="lastname" data-valmsg-replace="true"></span>
        </div>

现在我应该在脚本中使用什么标记来引发onFocus和onBlur事件 我试过用 “textbox”,“text-box single-line”使用这些和其他一些作为选择器,但它们正在工作,有些人帮我选择我应该使用

1 个答案:

答案 0 :(得分:2)

您可以使用元素和"attribute equals"选择器,例如:

$("input[type='text']").blur(function(e) {
  $(this).css("background", someColour);
});

将适用于所有文本框。