繁体   English   中英

jquery中的innerhtml等效函数不起作用

[英]innerhtml equivalent in jquery is not working

我的div没有显示我期待的内容

我的控制器动作:

    /// <summary>
    /// GetCountiresForManufacturer
    /// </summary>
    /// <returns></returns>
    [Authorize(Roles = "Administrator")]
    [AcceptVerbs(HttpVerbs.Get)]
    public string GetCountiresForManufacturer(string manufacturer)
    {
        var sb = new StringBuilder();
        var rows = new List<Manufacturer>();
        var data = new Dictionary<string, List<Country>>();
       // var countries = new List<Country>();
        if (manufacturer.StartsWith(","))
        {
            manufacturer = manufacturer.TrimStart(',');
        }
        string[] manufacturers = manufacturer.Split(',');
        foreach (var s in manufacturers)
        {
            var manufacturerRow = _manufacturerRepository.GetManufacturerByName(s);
            rows.Add(manufacturerRow);
        }

        foreach (var row in rows)
        {
            if(row==null)
            {
                continue;
            }
            var countriesList = _countryRepository
           .GetAllCountriesForManufacturer(row.Id);
           // countries.Add(countriesList);
            data.Add(row.Description, countriesList.ToList());
        }
        foreach (var s in data)
        {
            sb.Append(" <input id =\"Brand\"  name=\"Brand\" value=\""+s.Key+"\"/>");
            foreach (var manufacturer1 in s.Value)
            {
                sb.Append("<input id =\"Dove\" type=\"checkbox\" 
               value=\""+manufacturer1.Name+"\"/>");
            }
        }


        return sb.ToString();
    }

我的观点:

        <%@ Control Language="C#"  
    Inherits="System.Web.Mvc.ViewUserControl<EmsAdmin.Models.User>" %>
    <%@ Import Namespace="EmsAdmin.Models" %>

    <%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and  
      try again.") %>
    <% using (Html.BeginForm(new {}))
   {%>
  <%= Html.AntiForgeryToken() %>
 <%: Model.Id %>
<%= Html.HiddenFor(e=>e.SelectedBrands) %>

<p>
<%= Html.LabelFor(e=>e.Name,"Name:") %>
<%= Html.TextBoxFor(e => e.Name,  new { @style = "width: 255px;" })%>
<%= Html.ValidationMessageFor(e=>e.Name,"") %>
</p>
<p>
<%= Html.LabelFor(e=>e.Email,"Email:") %>
<%= Html.TextBoxFor(e => e.Email, new { @style = "width:250px;" })%>
<%= Html.ValidationMessageFor(e=>e.Email,"") %>
</p>
<p>
<%= Html.LabelFor(e=>e.Corporation.Description,"Corporation:") %>
<%= Html.TextBoxFor(e => e.Corporation.Description, new { @style = "width:250px;" })%>
<%= Html.ValidationMessageFor(e=>e.Corporation.Description,"") %>
</p>
   <hr />
 <h2>Approve User</h2>
    <p>
<%= Html.LabelFor(e=>e.Approve,"Aprrove User:") %>
<%= Html.CheckBoxFor(e=> e.Approve,Model.Approve) %><span>&nbsp;Switch this on if you
  want to
    add regitrar </span>
<%= Html.ValidationMessageFor(e=>e.Approve,"") %>
</p>
  <hr />
<h2>Multi Select Brands If Required:</h2>
<div id="checkboxes">
<% foreach (var manufacturer in Model.Manufacturer)
   {%>
 <input type="checkbox" id="<%: manufacturer.Id %>" class="checkbox" value="<%: 
 manufacturer.Description %>" /><%: manufacturer.Description %><br />
<%} %>
<input type="submit" value="UpdateBrands" class="brands" />
</div>
<div id="checkboxescountries" class="divNewCountriesList">

</div>
 <hr />
 <p>
<input type="submit" value="Save" />
<%= Html.ActionLink("Cancel","Details",new {id=Model.Id},new {@title="exit without
 saving"}) %>
 </p>
 <% } %>


    <script type="text/javascript">
    $(".brands").click(function(event) {
    event.preventDefault();
    var selected = "";
    var manu = "";
    var input = "";
    $("#checkboxes input:checked").each(function() {

        manu = $(this).val();
        selected = selected + "," + manu;
        alert(selected);
        $("#SelectedBrands").val(selected);
        alert($("#SelectedBrands").val());

    });
    var productInput = "";
    alert("I am here");
    var myUrl = "/Countries/GetCountiresForManufacturer/" + selected;
    alert(myUrl);
    $.ajax({
        url: myUrl,
        type: 'get',
        success: function(data) {
            productInput = data;
            alert(productInput);
           //$('.brands').after("<div id='divNewCountriesList" + id + "' class ='divNewCountriesList'"+" </div>");

            }
    });
    $(".divNewCountriesList").html(productInput);

});

注意:如果我给

$(".divNewCountriesList").html("Hello World");

我可以在div中看到但不能看到productInput。 不确定我在哪里做错了。

Ajax是一个异步请求。 所以你需要在ajax成功处理程序中设置div的html

$.ajax({
        url: myUrl,
        type: 'get',
        success: function(data) {
            productInput = data;
            alert(productInput);   
            $(".divNewCountriesList").html(productInput);
        }
    });

暂无
暂无

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

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