【问题标题】:jquery script (onchange method ) not work after return partial view as ajaxjquery脚本(onchange方法)在将部分视图作为ajax返回后不起作用
【发布时间】:2017-04-24 08:52:12
【问题描述】:

我的部分观点: 这是处理下拉列表更改的脚本;我想在更改后向控制器发送请求

@model Bekr.Helper.BekrJson

@{
    List<string> Types = new List<string>() { "متن", "عدد", "bool", "آبجکت", "آرایه" };
    var InputType = new SelectList(Types,Model.typ);
    var tt = $"detail{TempData["index"]}";
    var divtt = $"div{TempData["index"]}";
    }
<script src="~/Scripts/jquery-2.2.4.js"></script>
<script src="~/Scripts/jquery-2.2.4.intellisense.js"></script>

 <div class="col-md-12 q">

    <div class="col-md-5">
        @Html.TextBox($"[{(int)TempData["index"]}].key", Model.key, new { @class = "form-control", placeholder = "کلید را وارد نمایید..." })
    </div>

    <div class="col-md-2">
            @Html.DropDownList($"[{(int)TempData["index"]}].typ", InputType, new { style = "width:100%",id=$"{tt}" })
    </div>
    <div class="col-md-5">
        @Html.TextBox($"[{(int)TempData["index"]}].value", Model.value, new { @class = "form-control", placeholder = "مقدار را وارد نمایید...", style = "margin-top:1px;" })
    </div>

</div>
<div id="@divtt">

</div>

<script>
    
        $('#@tt').on('change', function () {
        $.ajax({
                url: "/Items/InputValueType/" + $('#@tt').val(),
                type: "GET",
                success: function (data) {
                    $('#@divtt').html(data);
                }
            })

        })
    
   
</script>

在此视图中返回部分视图:

@model Bekr.DB.cmn_Item
@using Bekr.Helper

@{ 
    TempData["JsonList"] = Model.KeyValueList.Count;
}



@using (Ajax.BeginForm((string)ViewBag.Action, "Items", null, new AjaxOptions { UpdateTargetId = "111", InsertionMode = InsertionMode.Replace, HttpMethod = "POST" }))
{
<div class="create-edit-item" id="">

        @Html.AntiForgeryToken()
        <button type="submit" class="btn btn-xs btn-success">ثبت</button>

        @Html.TextBoxFor(x => x.cmn_Items_Name, new { @class = "form-control", placeholder = "عنوان را وارد نمایید..." })

        @for (int i = 0;  Model.KeyValueList != null  && i < Model.KeyValueList.Count ; i++)
        {
            @Html.Action("EditSingleJson", new { listJson = Model.KeyValueList, index = i });
        }
        <div id="NewJson">

         
        </div>


        @Ajax.RawActionLink(string.Format("<i class='fa fa-plus' aria-hidden='true'></i>"+
        "<i class='fa fa-plus' aria-hidden='true'></i>"), "CreateSinglejson", "Items", new {jsonList=Model.KeyValueList }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "NewJson" }, new { @class = "btn btn-success btn-xs", id = " " })

     


            @Html.HiddenFor(x => x.cmn_Items_Type)
            @Html.HiddenFor(x => x.cmn_Items_Id)
        @Html.HiddenFor(x => x.cmn_Items_Info)
   
</div>


}

在 div 中加载 ajax 时,onchange 方法不适用于局部视图

【问题讨论】:

  • 将$('#@tt').on('change', function () {更改为$(document).on('change','#@tt',function(){
  • 谢谢你的回答,我做到了,但是问题没有解决
  • 不,此脚本适用于来自数据库的记录但在部分视图中创建新对象不起作用
  • 此部分视图有两个用途:1- 显示数据库记录 2- 创建新项目;在第一种情况下,脚本工作在第二种情况下,要添加的 ajax 不起作用
  • 我将 id 转换为类,但没有改变

标签: jquery ajax asp.net-mvc


【解决方案1】:

首先将id 转换为class,因为jQuery 无法识别多个相同的ID(jQuery 将id 视为单独的选择器)。

然后改变如下:-

$(document).on('change','.@tt',function(){

这叫:-event delegation

【讨论】:

  • 谢谢你的回答,我做到了,但是问题没有解决——
  • @masoudgharibdoust 将 id 转换为 class 然后检查,因为 jQery 无法识别多个相同的 id。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多