Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery.empty()有问题_Jquery_Html_Dom - Fatal编程技术网

jquery.empty()有问题

jquery.empty()有问题,jquery,html,dom,Jquery,Html,Dom,我使用jquery用一个包含web服务数据的表来填充div,但是我注意到,如果我重新加载页面,div并没有被过度写入,而是被附加到其中。我查找了jquery.empty()函数,但它似乎不适合我 以下是我的代码示例: Div标签 <div id="AccountGrid" class="AccountGridHolder"> </div> jquery函数 function GetAcount() { $('#AccountGrid').empty()

我使用jquery用一个包含web服务数据的表来填充div,但是我注意到,如果我重新加载页面,div并没有被过度写入,而是被附加到其中。我查找了jquery.empty()函数,但它似乎不适合我

以下是我的代码示例:

Div标签

<div id="AccountGrid" class="AccountGridHolder">

</div>
jquery函数

function GetAcount() {
    $('#AccountGrid').empty()
    var CompanyID = get_localstorage("CompanyID");
        source:
            {
                $.ajax({
                      crossDomain: true,
                      url: "https://webservice/methode",
                      type: "POST",
                      dataType: "json",
                      contentType: "application/json; charset=utf-8",
                      data: "{ 'CompanyID' : '" + CompanyID + "'}",
                      dataFilter: function (data) { return data; },
                      success: function (data) {
                          var Table1 = '<fieldset><legend>Company Name</legend><table id="CompanyRow" class="AccountGrid" ><tr><td>';
                          Table1 = Table1 + data.d[0] + '</td></tr></table></feildset>';
                          $(".AccountGridHolder").append(Table1);
                      },
                      error: function (Error) {
                          console.log('CompanyID (fail) = ' + CompanyID);
                          console.log(Error.status);
                          console.log(Error.statusText);
                          console.log(Error.responseText);
                      }
                  });
              }
          }
函数GetAcount(){
$('#AccountGrid').empty()
var CompanyID=获取本地存储(“CompanyID”);
资料来源:
{
$.ajax({
跨域:是的,
url:“https://webservice/methode",
类型:“POST”,
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
数据:“{'CompanyID':'”+CompanyID+“}”,
dataFilter:函数(数据){返回数据;},
成功:功能(数据){
变量表1='公司名称';
Table1=Table1+data.d[0]+'';
$(“.AccountGridHolder”)。追加(表1);
},
错误:函数(错误){
console.log('CompanyID(fail)='+CompanyID);
console.log(错误状态);
console.log(Error.statusText);
console.log(Error.responseText);
}
});
}
}
有人知道我做错了什么吗?

使用
html()
代替
append()
它将替换div的内容

$(".AccountGridHolder").html(Table1);
尝试使用.html()而不是.append()


你的选择器错了。如果要按id匹配,则它应该是
$(“#AccountGrid”)

$(".AccountGridHolder").empty();
$(".AccountGridHolder").append(Table1);
相当于

$(".AccountGridHolder").html(Table1);

那么为什么要写两行而不是一行呢?选择后者。

我不知道为什么,但当我移动空()行时,它开始工作了。。。这是我的密码

function GetAcount() {
            var CompanyID = get_localstorage("CompanyID");
        source:
            {
                $.ajax({
                      crossDomain: true,
                      url: "https://webservice/methode",
                      type: "POST",
                      dataType: "json",
                      contentType: "application/json; charset=utf-8",
                      data: "{ 'CompanyID' : '" + CompanyID + "'}",
                      dataFilter: function (data) { return data; },
                      success: function (data) {
                          $('#AccountGrid').empty();
                          var Table1 = '<fieldset><legend>Company Name</legend><table id="CompanyRow" class="AccountGrid" ><tr><td>';
                          Table1 = Table1 + data.d[0] + '</td></tr></table></fieldset>';
                          $(".AccountGridHolder").append(Table1);
                      },
                      error: function (Error) {
                          console.log('CompanyID (fail) = ' + CompanyID);
                          console.log(Error.status);
                          console.log(Error.statusText);
                          console.log(Error.responseText);
                      }
                  });
              }
          }
函数GetAcount(){
var CompanyID=获取本地存储(“CompanyID”);
资料来源:
{
$.ajax({
跨域:是的,
url:“https://webservice/methode",
类型:“POST”,
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
数据:“{'CompanyID':'”+CompanyID+“}”,
dataFilter:函数(数据){返回数据;},
成功:功能(数据){
$('#AccountGrid').empty();
变量表1='公司名称';
Table1=Table1+data.d[0]+'';
$(“.AccountGridHolder”)。追加(表1);
},
错误:函数(错误){
console.log('CompanyID(fail)='+CompanyID);
console.log(错误状态);
console.log(Error.statusText);
console.log(Error.responseText);
}
});
}
}

我觉得这真的很奇怪,因为这表明问题在于div没有加载,但这段代码只在“pagecontainershow”事件触发后执行

啊,没错,但这并没有解决问题。明天我会好好玩玩,看看我能不能把它修好。现在发生了什么?相同的事情还是不同的事情?完全相同的事情仍在发生使用console.log()我可以看到#AccountGrid正在选择正确的元素。它只是.empty出于某种原因没有做任何事情,我不是在告诉你不要使用
ajax
调用。我只是说,奇怪的是,你把它包装在一个有标签的代码块中:
source:{…}
。可能不会对您造成伤害,但这很奇怪,一些浏览器(至少在历史上)没有一致地处理标记块。因为您可能复制和粘贴了字段集,所以需要更正字段集的拼写。结束标签拼错了。谢谢你指出这一点
$(".AccountGridHolder").html(Table1);
function GetAcount() {
            var CompanyID = get_localstorage("CompanyID");
        source:
            {
                $.ajax({
                      crossDomain: true,
                      url: "https://webservice/methode",
                      type: "POST",
                      dataType: "json",
                      contentType: "application/json; charset=utf-8",
                      data: "{ 'CompanyID' : '" + CompanyID + "'}",
                      dataFilter: function (data) { return data; },
                      success: function (data) {
                          $('#AccountGrid').empty();
                          var Table1 = '<fieldset><legend>Company Name</legend><table id="CompanyRow" class="AccountGrid" ><tr><td>';
                          Table1 = Table1 + data.d[0] + '</td></tr></table></fieldset>';
                          $(".AccountGridHolder").append(Table1);
                      },
                      error: function (Error) {
                          console.log('CompanyID (fail) = ' + CompanyID);
                          console.log(Error.status);
                          console.log(Error.statusText);
                          console.log(Error.responseText);
                      }
                  });
              }
          }