2013-03-11 65 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

相關問題