2014-05-17 62 views
0
<table>  
      @{int counter = @ViewBag.counter, j = 0, k = 0; } 

      <tr> 
       <td> 
        @Html.EditorFor(model => model.PortTariffList[counter].StartRate, new { htmlAttributes = new { size = 5, maxLength = 10 } }) 
        | 
         @Html.EditorFor(model => model.PortTariffList[counter].EndRate, new { htmlAttributes = new { size = 5, maxLength = 10 } }) 
       </td> 

       @foreach (var tlist in Model.PortTariffList) 
       { 
        if (counter == k) 
        { 
         foreach (var amount in tlist.list) 
         { 
        <td>@Html.EditorFor(model => model.PortTariffList[counter].list[j].Amount, new { htmlAttributes = new { size = 6, maxLength = 10 } }) </td> 
        @Html.HiddenFor(model => model.PortTariffList[counter].list[j].TerminalId)      
        @Html.Hidden("PortTariffList.Index", counter)  
          j++; 
         } 
        } k++; 
       } 
       <td> 
        <input type="button" value="Remove" name="Remove" class="RemoveLink small" data-id="@counter" /> 
       </td> 
      </tr> 

</table> 

創建動態文本框,但是如何獲取jquery中所有行的開始和結束價值?如何獲得asp.net mvc動態文本框的值爲

+0

嘗試爲每個'textbox'或類似'$('table input [type = text]')。each(function(){$(this).val()});' –

+0

我有每行比較開始和結束率文本框? –

回答

0

試試這個:

View添加id屬性

@Html.EditorFor(model => model.PortTariffList[counter].StartRate, new { size = 5, maxLength = 10,id="txtStartRate_"+j}) 

@Html.EditorFor(model => model.PortTariffList[counter].EndRate, new {size = 5, maxLength = 10,id="txtEndRate_"+j}) 

的Javascript:本scirpt

<script language="javascript" type="text/javascript"> 
$(doucument).ready(function() 
{ 
    $("input[id^="txtStartRate_"]").each(function(i) 
      { 
       //get start and end rate values and compare 
       var st=$(this).val(); 
       var end=$("#txtEndRate_"+i).val(); 
      }); 
    //text box change event handler 
    $("input[id^="txtStartRate_"],input[id^="txtEndRate_"]").change(function() 
      { 
       var curId=$(this).attr('id'); 
       var curVal=$(this).val(); 
       var stIndex=curId.indexOf('txtStartRate'); 
       var startRate=0; 
       var endRate=0; 
       if(stIndex!=-1) 
        { 
        var co=curId.split('_')[1]; 
        //this end rate value 
        endRate=$('txtEndRate_'+co).val(); 
        //curVal is Start rate value 
         startRate=curVal; 
        } 
       else 
        { 
        var co=curId.split('_')[1]; 
        //this startrate value 
        startRate=$('txtStartRate_'+co).val(); 
        //curVal is Endrate value 
         endRate=curVal; 
         } 
        //compare  
        if(startRate>endRate) 
         { 
          alert('start rate is greater than end rate"); 
         } 
        else 
         { 
           alert('end rate is greater than start rate"); 
         } 
      }); 

}); 
+0

改變文本框的值時的開始和結束率比較。有關情況的任何想法? –

+0

在solution.Check中添加更改事件處理程序一次 – malkam

+0

感謝malkam,但我沒有比較開始和結束率,我的方案是,我爲每行和每行開始和結束生成新的文本框的開始和結束率比率與另一個開始和結束率,任何想法呢? –

0
var isfound = false; 
    var liststart = new Array();   
    var listend = new Array(); 

    $(".StartRate").blur(function (e) { 

     $("input[class^='StartRate']").each(function (i) { 
      var start = $("#txtStartRate_" + i).val(); 
      var end = $("#txtEndRate_" + i).val(); 

      liststart[i] = start; 
      listend[i] = end; 

     }); 


     if (liststart.length > 0) { 
      var startrate = 0; 
      var endrate = 0; 


      for (var i = 0; i < liststart.length; i++) 
      { 
       if ((liststart[i] == startrate) && (listend[i] == endrate)) { 

        isfound = true; 
       } 
       else { 
        startrate = liststart[i]; 
        endrate = listend[i]; 
       } 
      } 


     } 


     $("#sameRange").empty(); 
     if (isfound) { 
      $("#sameRange").append("You have enter the same range.").show(); 
      $("#sameRange").addClass("alert error"); 
      isfound = false; 
     } 
     else 
     { 
      $("#sameRange").empty(); 
      $("#sameRange").removeClass("alert error"); 
      isfound = false; 

     } 


    }); 

    if (isfound == false) { 


     liststart = new Array(); 
     listend = new Array(); 

     $(".EndRate").blur(function (e) { 


      $("input[class^='EndRate']").each(function (i) { 
       var start = $("#txtStartRate_" + i).val(); 
       var end = $("#txtEndRate_" + i).val(); 

       liststart[i] = start; 
       listend[i] = end; 
      }); 

      if (liststart.length > 0) { 
       var startrate = 0; 
       var endrate = 0; 

       for (var i = 0; i < liststart.length; i++) { 

        if ((liststart[i] == startrate) && (listend[i] == endrate)) { 
         isfound = true; 
        } 
        else { 
         startrate = liststart[i]; 
         endrate = listend[i]; 
        } 
       } 
      } 


      $("#sameRange").empty(); 
      if (isfound) { 
       $("#sameRange").append("You have enter the same range.").show(); 
       $("#sameRange").addClass("alert error"); 
       isfound = false; 
      } 
      else { 
       $("#sameRange").empty(); 
       $("#sameRange").removeClass("alert error"); 
       isfound = false; 
      } 


     }); 
    } 

我有固定的問題

相關問題