2013-03-11 67 views
0

我这里有这个观点基于一个单选按钮的状态,这会影响textboxfields:脚本鉴于只有在列表中影响一个项目

<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> 

回答

1

ID是单数。只有一个项目可以拥有该ID。

你将不得不在你的选择器中使用类名,你将不得不建立逻辑来选择正确的项目。

+0

哦,所以如果我嘲笑每个项目而不是一个ID的类,这将工作? – hsim 2013-03-11 16:55:04

+0

这是一个类ID和一个ID,这是说。 – hsim 2013-03-11 16:58:11

+0

我用class替换了ID标签,现在一切正常。谢谢你的提示! – hsim 2013-03-11 17:10:51

相关问题