我有这个视图,其中有 2 个单选按钮强类型化到模型中,我希望根据这些单选按钮的状态启用/禁用文本框字段。
这是我现在正在处理的视图和脚本:
@model IList<MyApp.Models.ObjInfo>
@{
ViewBag.Title = "SendItems";
}
<h2>Ebay Items</h2>
<script src="/Scripts/jquery-1.7.1.min.js"
type="text/javascript"></script>
<script type="text/javascript">
function dostate1() {
$("#textfield1").attr("disabled", "disabled");
$("#textfield2").removeAttr("disabled");
$("#textfield3").removeAttr("disabled");
}
function dostate2() {
$("#textfield1").removeAttr("disabled");
$("#textfield2").attr("disabled", "disabled");
$("#textfield3").attr("disabled", "disabled");
}
$(document).ready(function ()
{
alert("The document is ready");
if ($("#state1").is(":checked")) {
dostate1();
} else {
dostate2();
}
$("#state1").click(function (){
alert("Auction radio button has been clicked");
dostate1();
});
$("#state2").click(function () {
alert("Buy It Now radio button has been clicked");
dostate2();
});
});
</script>
<p>
@using (Html.BeginForm("ManageItems", "Item Inventory"))
{
(...)
@for (int i = 0; i < Model.Count; i++)
{
<p>
<tr>
<td>@Html.DisplayFor(x => x[i].m_OtrObj.m_ObjName)</td>
<td>@Html.RadioButtonFor(x => x[i].m_State, "State 1", new {id = "state1", style ="width: 50px"})</td>
<td>@Html.RadioButtonFor(x => x[i].m_State, "State 2", new {id = "state2", style ="width: 50px"})</td>
<td>
@Html.TextBoxFor(x => x[i].m_Field1, new{id = "textField1", style = "width:200px"})
</td>
<td>
@Html.TextBoxFor(x => x[i].m_Field2, new {id = "textField2", style = "width:200px"})
</td>
<td>
@Html.TextBoxFor(x => x[i].m_Field3, new {id ="textField3", style = "width:200px" })
</td>
</tr>
</p>
}
</table>
<input type="submit" value="Do Something"/>
}
</p>
现在我有两个主要问题:
- 单击每个单选按钮实际上会禁用我希望禁用的字段,但不要激活其他字段;
- 该脚本实际上仅在单击按钮时运行,但应在开始时运行以避免字段 1 处于活动状态,因为默认情况下启用“状态 1”单选按钮。
我真的是 javascript 的新手,所以任何人都可以帮助我吗?谢谢!!
编辑 **
我已经修改了脚本以向您展示迄今为止的演变,感谢所有提供帮助的人,脚本有效,但仅适用于列表中的第一项。考虑到它是一个对象列表(请参阅@model),我如何单独影响列表中的每个项目?