如何获取asp.net mvc动态文本框值

时间:2014-05-17 16:58:00

标签: jquery asp.net-mvc

<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中所有行的开始和结束速率值?

2 个答案:

答案 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:

<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");
                         }
            });

});

答案 1 :(得分: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;
            }


        });
    }

我修复了这个脚本的问题