gpt4 book ai didi

javascript - 如何通过ajax追加div

转载 作者:行者123 更新时间:2023-11-29 10:32:36 25 4
gpt4 key购买 nike

我正在尝试从数据库中检索值,之后我如何从该静态方法传递这些值并附加到 ajax 中的 div,这是 C# 代码

[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();
}

这里是ajax方法

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

谁能解释一下我该如何解决这个问题

最佳答案

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

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

或者您也可以使用加法赋值运算符 += 代替赋值运算符 = 将结果附加到 div_ClientLoc.innerHTML 中

div_ClientLoc.innerHTML += response.d;

关于javascript - 如何通过ajax追加div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42546934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com