我在这里有这个视图,它会根据单选按钮的状态影响文本框字段:
<script src="/Scripts/jquery-1.7.1.min.js"
type="text/javascript"></script>
<script type="text/javascript">
function doState1() {
alert("state1 radio button has been clicked");
$("#field1").attr("disabled", "disabled");
$("#field2").removeAttr("disabled");
$("#field3").removeAttr("disabled");
}
function doState2() {
alert("state2 radio button has been clicked");
$("#field1").removeAttr("disabled");
$("#field2").attr("disabled", "disabled");
$("#field3").attr("disabled", "disabled");
}
$(document).ready(function ()
{
alert("The document is ready");
if ($("#state1").is(":checked")) {
doState1();
} else {
doState2();
}
$("#state1").click(function (){
doState1();
});
$("#state2").click(function () {
doState();
});
});
</script>
主要问题是我的视图由项目列表填充,并且只有列表中的第一项实际受此脚本影响。为什么?我试过在这里和那里放(这个)声明,认为它会影响提到的项目,但它并没有改变任何事情。
这是 for 循环,以便您了解实际发生的情况:
<p>
@using (Html.BeginForm("ConfirmSendItems", "Inventory"))
{
(...)
<table>
(...)
@for (int i = 0; i < Model.Count; i++)
{
<p>
<tr>
<td>@Html.DisplayFor(x => x[i].otrObj.objName)</td>
<td>@Html.RadioButtonFor(x => x[i].m_state1, "State 1", new {id = "state1", style ="width: 50px"})</td>
<td>@Html.RadioButtonFor(x => x[i].m_state1, "State 2", new {id = "state2", style ="width: 50px"})</td>
<td>
@Html.TextBoxFor(x => x[i].m_Field1, new{id = "field1", style = "width:200px"})
</td>
<td>
@Html.TextBoxFor(x => x[i].m_Field2, new {id = "field2", style = "width:200px"})
</td>
<td>
@Html.TextBoxFor(x => x[i].m_Field3, new {id ="field3", style = "width:200px" })
@Html.ValidationMessageFor(x => x[i].m_FixedPrice, "Fixed Price must be a number.")
</td>
(...)
</p>
}
</table>
<input type="submit" value="Ready!"/>
}
</p>