2015-02-11 97 views
1

我試圖從項目 ivanz.com/2011/06/16/editing-variable-length-reorderable-collections-in-asp-net-mvc-part-1/ASP MVC集合編輯

這是一個很好的例子,但我不知道如何如何獲得總評 ,因爲ID是這樣

'FavouriteMovies_98812e66-80f2-453b-a0a8-bf85d8bc6ae0__Rating' 

通常如果ID就像是

'FavouriteMovies_0__Rating' 
'FavouriteMovies_1__Rating' 
'FavouriteMovies_2__Rating' 

我使用JavaScript

這樣

for(var i = 0; i <= rowCount-1;i++){ 
summary += $('#FavouriteMovies_' + i + '__Rating').val(); 
} 

請任何人都可以幫我

這是我的HTML的完整標籤

<fieldset> 
 
     <legend>My Favourite Movies</legend> 
 

 
     <ul style="list-style-type: none" id="movieEditor" class="ui-sortable"> 
 
<li style="padding-bottom:15px"> 
 
<input type="hidden" value="b231eea2-79fc-499d-a286-7d30dbc3be9c" name="FavouriteMovies.Index" autocomplete="off"> 
 
     <img alt="" style="cursor: move" src="/Content/images/draggable-icon.png"> 
 
<label for="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Title">Title</label><input type="text" value="Movie 1" name="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Title" id="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Title" data-val-required="The Title field is required." data-val="true" class="text-box single-line"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Title" class="field-validation-valid"></span><label for="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Rating">Rating</label><input type="text" value="5" name="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Rating" id="FavouriteMovies_b231eea2-79fc-499d-a286-7d30dbc3be9c__Rating" data-val-required="The Rating field is required." data-val-number="The field Rating must be a number." data-val="true" class="rating"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[b231eea2-79fc-499d-a286-7d30dbc3be9c].Rating" class="field-validation-valid"></span>  <a onclick="$(this).parent().remove();" href="#">Delete</a> 
 
</li> 
 
<li style="padding-bottom:15px"> 
 
<input type="hidden" value="5d85f979-0b96-4ba5-9f7a-a1ffe25d406a" name="FavouriteMovies.Index" autocomplete="off"> 
 
     <img alt="" style="cursor: move" src="/Content/images/draggable-icon.png"> 
 
<label for="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Title">Title</label><input type="text" value="Movie 2" name="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Title" id="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Title" data-val-required="The Title field is required." data-val="true" class="text-box single-line"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Title" class="field-validation-valid"></span><label for="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Rating">Rating</label><input type="text" value="10" name="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Rating" id="FavouriteMovies_5d85f979-0b96-4ba5-9f7a-a1ffe25d406a__Rating" data-val-required="The Rating field is required." data-val-number="The field Rating must be a number." data-val="true" class="rating"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[5d85f979-0b96-4ba5-9f7a-a1ffe25d406a].Rating" class="field-validation-valid"></span>  <a onclick="$(this).parent().remove();" href="#">Delete</a> 
 
</li> 
 
<li style="padding-bottom:15px"> 
 
<input type="hidden" value="57fb472a-ae66-427a-8a46-0af771d8997e" name="FavouriteMovies.Index" autocomplete="off"> 
 
     <img alt="" style="cursor: move" src="/Content/images/draggable-icon.png"> 
 
<label for="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Title">Title</label><input type="text" value="Movie 3" name="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Title" id="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Title" data-val-required="The Title field is required." data-val="true" class="text-box single-line"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Title" class="field-validation-valid"></span><label for="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Rating">Rating</label><input type="text" value="12" name="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Rating" id="FavouriteMovies_57fb472a-ae66-427a-8a46-0af771d8997e__Rating" data-val-required="The Rating field is required." data-val-number="The field Rating must be a number." data-val="true" class="rating"><span data-valmsg-replace="true" data-valmsg-for="FavouriteMovies[57fb472a-ae66-427a-8a46-0af771d8997e].Rating" class="field-validation-valid"></span>  <a onclick="$(this).parent().remove();" href="#">Delete</a> 
 
</li> 
 
     </ul> 
 
     <input type="text" value="" name="SummaryRating" id="SummaryRating"> 
 

 
     <a href="#" id="addAnother">Add another</a> 
 
     <script type="text/javascript"> 
 
      $(function() { 
 
       $("#movieEditor").sortable(); 
 

 
       function reAmount() { 
 
        $.get('/User/MovieRatingSummaryColumn', function (data) { 
 
         $("#SummaryRating").val(data.sum); 
 
        }); 
 
       } 
 

 
       $(".rating").on('change', function() { 
 
        alert(this.id); 
 
        reAmount(); 
 
       }); 
 

 
       $("#addAnother").click(function() { 
 
        $.get('/User/MovieEntryRow', function (template) { 
 
         $("#movieEditor").append(template); 
 
        }); 
 

 
       }); 
 
      }); 
 
     </script> 
 
    </fieldset>

+0

總結什麼?你想做什麼? – 2015-02-11 09:09:42

+0

計數總計等級 sory我的英文不夠好 – Chris 2015-02-11 09:21:33

+0

您是否試圖訪問表格中的元素?如果是這樣,顯示錶中的一行的HTML(你不需要使用'ID') – 2015-02-11 09:24:55

回答

0

爲了得到總的所有您可以使用的Rating屬性的輸入

var total = 0; 
$('.rating').each(function() { 
    total += new Number($(this).val()); 
}); 
+0

它正在工作非常感謝你.. – Chris 2015-02-11 09:58:55