如何通过ajax追加div

i'm trying to retrieve values from database after that how can i pass those values from that static method and append to div in ajax ,Here is the C# code for that

[WebMethod]
    [ScriptMethod]
    public static string[] Cdetails(Cmpny cmpny)
    {
        StringBuilder html = new StringBuilder();
        string strCname = cmpny.Cname;
        string strCvalue= cmpny.Cvalue;
        string strLvl = cmpny.Clevel;
        int intLvl = Convert.ToInt32(strLvl);
        List<string> company = new List<string>();
        if (intLvl == 1)
        {


           SqlConnection conn = new SqlConnection(HttpContext.Current.Session["ConnectionString"].ToString());
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select Clientid,C_Name,C_website,Status,CreatedDate,CreatedBy,ModifiedDate,ModifiedBy from clientDetails where Clientid=@Clientid and C_Name=@C_Name";
                cmd.Parameters.AddWithValue("@C_Name", strCname);
                cmd.Parameters.AddWithValue("@Clientid", strCvalue);
                cmd.Connection = conn;
                conn.Open();

                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        company.Add(string.Format("{0}-{7}", sdr["Clientid"], sdr["C_Name"], sdr["C_website"], sdr["Status"], sdr["CreatedDate"], sdr["CreatedBy"], sdr["ModifiedDate"], sdr["ModifiedBy"]));
                    }
                }
                conn.Close();
                }



        }
        //else
        //{
        //    return strLvl.ToArray();
        //}

        return company.ToArray();
    }

and ajax method is here

 <script type="text/javascript">
    function OnTreeClick(evt) {
        var src = window.event != window.undefined ? window.event.srcElement : evt.target;
        var nodeClick = src.tagName.toLowerCase() == "a";

        if (nodeClick) {

            var nodeText = src.innerText || src.innerHTML;
            var nodeValue = GetNodeValue(src);

            var nodePath = src.href.substring(src.href.indexOf(",") + 2, src.href.length - 2);
            //alert(nodePath.toLowerCase());

                if (nodePath.indexOf("\\") > -1)
                {
                    var level = "2";
                  //  alert("Second level ");
                }
                else
                {
                    var level = "1";
                  //  alert(" first level ");
                }


            //var nodestat = 
            alert("Text: " + nodeText + "," + "Value: " + nodeValue );
            var cmpny = {};
            cmpny.Cname = nodeText;
            cmpny.Cvalue = nodeValue;
            cmpny.Clevel = level;
            $.ajax({
                type: "POST",
                url: "CS.aspx/Cdetails",
                data: '{cmpny: ' + JSON.stringify(cmpny) + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    div_ClientLoc.innerHTML = response.d;//here how can i append those tag and div_ClientLoc is div id
                   // window.location.reload();
                }
            });
            return false;
        }
        return true;
    }
    function GetNodeValue(node) {
        var nodeValue = "";
        //alert(node.href.toLowerCase());
        var nodePath = node.href.substring(node.href.indexOf(",") + 2, node.href.length - 2);
       // alert(nodePath.toLowerCase());
        var nodeValues = nodePath.split("\\");
        if (nodeValues.length > 1)
            nodeValue = nodeValues[nodeValues.length - 1];
        else
            nodeValue = nodeValues[0].substr(1);

        return nodeValue;
    }

can anyone please explain me how can i solve this problem

2个回答


您正在通过将结果分配给 innerHTML </ code>来覆盖 div_ClientLoc </ code>的html。 您可以使用jQuery append()方法将响应结果附加到 div_ClientLoc的现有html中 </ code>。</ p>

  $(“#div_ClientLoc”)。append(response.d);
</ code> </ pre>

或者您也可以通过使用加法赋值运算符 + = </ code>代替赋值运算符 = </ code>来将结果附加到div_ClientLoc.innerHTML中,如下所示:</ p>

  div_ClientLoc.innerHTML + = response.d;
</ code> </ pre>
     </ div>

展开原文

原文

You are overwriting the html of div_ClientLoc by assigning the result to innerHTML. You can use jQuery append() method to append the response result in existing html of div_ClientLoc.

$("#div_ClientLoc").append(response.d);

Or you can also append the result in div_ClientLoc.innerHTML by using Addition Assignment Operator += instead of assignment operator = like as under

div_ClientLoc.innerHTML += response.d;


您需要保留要添加到的元素,然后插入HTML。
您可以使用 document.getElementById('div_ClientLoc')</ code>来获取目标元素。
insertAdjacentHTML </ code>会将内容插入在结束标记之前的 beforeend </ code>处。</ p>

document.getElementById('div_ClientLoc')。insertAdjacentHTML('beforeend',response.d); </ code> </ p>
     </ div>

展开原文

原文

You need to get a hold of the element that you are appending to, then insert the HTML. You can use document.getElementById('div_ClientLoc') to get your target element. insertAdjacentHTML will insert the content at beforeend, which is before the closing tag.

document.getElementById('div_ClientLoc').insertAdjacentHTML('beforeend', response.d);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐