简体   繁体   English

jQuery文本框更改事件未触发,MVC

[英]jQuery textbox change event doesn't fire, mvc

I am trying to fire a method in JQuery, but it won't work. 我正在尝试在JQuery中触发一个方法,但是它不起作用。

 @using (Html.BeginForm("AddOrUpdateMethod", "Controller", FormMethod.Post))
{
     //some code
     <div class="row">

            <div class="period-list">
                <span class="advance-season">
                    <span class="glyphicon glyphicon-warning-sign margin-right10"></span>Something
                </span>
                @for (int i = 0; i < Model.Periods.Count; i++)
                {
                    <div class="form-group period">
                        @Html.HiddenFor(model => model.Periods[i].Id, new { @Value = Model.Periods[i].Id })                            
                        <div class="pull-left inline-block col-xs-5 col-sm-5 col-md-5 col-lg-5">                                
                            <div class="text-box-with-icon calendar margin-bottom10">
                                    @Html.TextBoxFor(model => model.Periods[i].From, "{0:dd/MM/yyyy}", new { @class = "form-control period-from", @onchange = "periodChange();" })                                    
                                <span class="icon"></span>
                            </div>
                        </div>
                        <div class="pull-left inline-block col-xs-5 col-sm-5 col-md-5 col-lg-5">                              
                            <div class="text-box-with-icon calendar margin-bottom10">                                   
                                    @Html.TextBoxFor(model => model.Periods[i].To, "{0:dd/MM/yyyy}", new { @class = "form-control period-to" })
                                <span class="icon"></span>
                            </div>
                        </div>
                 }
                 //some code
            </div>
       </div>

}

And my jQuery: 而我的jQuery:

<script type="text/javascript">
function periodChanged() {
    $('.period-from .period-to').change(function () {
        alert("click");
        //some code
   });
}

As you can see I tried already adding @change to textBox control. 如您所见,我已经尝试将@change添加到textBox控件中。 Nothing is happening. 没事 Maybe I should mention as well, that i I am using validation on model.Periods.From and model.Periods.To. 也许我也应该提到,我正在对model.Periods.From和model.Periods.To使用验证。 Moreover this view is a partial view. 此外,该视图是局部视图。

EDIT: I changed into: 编辑:我更改为:

<script type="text/javascript">
$(document).ready( function () {
    $('.period-from .period-to').click(function () {
        alert("click");
        if (Model.Periods.All(p=>p.To.Day - p.From.Day + 1 <= 7)) {

        }
    });
});

but it still doesn't work. 但它仍然不起作用。

    @using (Html.BeginForm("AddOrUpdateMethod", "Controller", FormMethod.Post))
{
     //some code
     <div class="row">

            <div class="period-list">
                <span class="advance-season">
                    <span class="glyphicon glyphicon-warning-sign margin-right10"></span>Something
                </span>
                @for (int i = 0; i < Model.Periods.Count; i++)
                {
                    <div class="form-group period">
                        @Html.HiddenFor(model => model.Periods[i].Id, new { @Value = Model.Periods[i].Id })                            
                        <div class="pull-left inline-block col-xs-5 col-sm-5 col-md-5 col-lg-5">                                
                            <div class="text-box-with-icon calendar margin-bottom10">
                                    @Html.TextBoxFor(model => model.Periods[i].From, "{0:dd/MM/yyyy}", new { @class = "form-control period-from", @onchange = "periodChange();" })                                    
                                <span class="icon"></span>
                            </div>
                        </div>
                        <div class="pull-left inline-block col-xs-5 col-sm-5 col-md-5 col-lg-5">                              
                            <div class="text-box-with-icon calendar margin-bottom10">                                   
                                    @Html.TextBoxFor(model => model.Periods[i].To, "{0:dd/MM/yyyy}", new { @class = "form-control period-to" })
                                <span class="icon"></span>
                            </div>
                        </div>
                 }
                 //some code
            </div>
       </div>

}

@*call the jquery.js file in the main or here*@

<script type="text/javascript">
$(document.ready(function(){

    $('.period-from .period-to').change(function () {
        alert("click");
        //some code
   });

});
</script>

this will fire the alert , but this is not enough for your case, you have to do more than this, as the list is generated in loop 这将触发警报,但这还不足以满足您的情况,因为列表是循环生成的,因此您还需要做更多的事情

your partial view should look like this, attaching the event after the C# code 您的局部视图应如下所示,将事件附加在C#代码之后

Try this way 试试这个

 $(document).on('change','.period-from .period-to') {
    alert("click");
    //some code
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM