Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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
Php 使用变量传递id的jquery remove()。可能的_Php_Jquery_Dom_Asynchronous - Fatal编程技术网

Php 使用变量传递id的jquery remove()。可能的

Php 使用变量传递id的jquery remove()。可能的,php,jquery,dom,asynchronous,Php,Jquery,Dom,Asynchronous,这就是函数 function deleteItem(id) { $.post("_asyncpage.php", {id:id}, function (result) { if (result == true) { $('#'+id).remove(); } }, "json"); } 因此,为了解释,函数接收一个id,发送到一个在db上执行随机内容的页面,

这就是函数

function deleteItem(id) {
    $.post("_asyncpage.php",
        {id:id},
        function (result) {
            if (result == true) {
                $('#'+id).remove();
            }
        }, "json"); 
}
因此,为了解释,函数接收一个id,发送到一个在db上执行随机内容的页面,并返回true/false

如前所述,函数内部检查结果是否为真/假。 如果为true,则继续删除与传递的id匹配的dom元素

数据库已正确更新,但.remove()无法工作。。。有人知道为什么(

下面是html结构的一个示例。TD中的表是要删除的表

<td width="120" valign="top" id="13_02">
   <table cellspacing="0" cellpadding="0" class="tableProg" id="1">
   <tbody>
      <tr>
         <td colspan="3"><h4 style="margin: 0pt;">Title</h4></td>
      </tr>
      <tr>
         <td colspan="3">h. 13:35</td></tr><tr><td width="74"><span style="font-weight: bold; color: rgb(0, 102, 204);">Su</span>: TV</td>
         <td width="22"><a href="javascript:openEditItem('2010/08/24','1')"><img src="static/images/edit.gif"></a></td>
         <td width="22"><a href="javascript:deleteItem('1')"><img src="static/images/delete.gif"></a></td>
      </tr>
   </tbody>
   </table>
</td>

标题
h、 13:35苏:电视

我会检查您的
结果
。您是以数字(1)的形式返回
true
,还是在收到时返回字符串

由于您的数据类型是
json
result
的值必须是
json字符串
或已解析的
对象
,因此我敢打赌

if (result == true) {
将永远不会通过。

是否执行
$('#'+id.).remove();

如果执行
警报($('#'+id).length)
,会发生什么情况?您应该希望得到一个>0的答案


另外,什么是
结果类型
?尝试
警报(typeof(result));
这将帮助您确定您的
的正确比较。如果
检查更新:根据您提供的其他信息,问题可能与您的ID有关。ID以数字开头无效。这可能会导致问题


如果要传递给函数的
id
在开头已经有一个
#
,则不希望将其串联

另外,如果您得到的响应是字符串,那么您需要将结果与字符串
“true”
进行比较

if (result == 'true') {...

为了提供更多信息,这是需要通过jquery删除的结构的一部分

<td width="120" valign="top" id="13_02">
   <table cellspacing="0" cellpadding="0" class="tableProg" id="1">
   <tbody>
      <tr>
         <td colspan="3"><h4 style="margin: 0pt;">Title</h4></td>
      </tr>
      <tr>
         <td colspan="3">h. 13:35</td></tr><tr><td width="74"><span style="font-weight: bold; color: rgb(0, 102, 204);">Su</span>: TV</td>
         <td width="22"><a href="javascript:openEditItem('2010/08/24','1')"><img src="static/images/edit.gif"></a></td>
         <td width="22"><a href="javascript:deleteItem('1')"><img src="static/images/delete.gif"></a></td>
      </tr>
   </tbody>
   </table>
</td>

标题
h、 13:35苏:电视

您能给出一个您正在传递的
id
以及
结果
响应的示例吗?id总是一个数字(当然是通过参数作为字符串传递的)。结果总是true/false布尔类型。我已经检查过函数是否通过if而不是执行remove()。在HTML4中,ID不能以数字开头。它无效。噢,我以为问题出在数字上。谢谢你的提示。
result
不太可能包含字符串,如果它是
json字符串,那么它应该是。函数$.post已使用属性(“json”)调用这意味着它将结果解析为jsonObject,因此true/false已自动从string转换为boolean。我说的是废话?虽然我会尝试另一种解决方案,但我同意你的看法。我希望有更多的声誉来给你投票:(希望我的感谢足够了:)@theCrius-那就足够了。:o)@theCrius-顺便说一下,@jameswiseman所说的是测试jQuery对象的
length
属性。这将告诉您找到了多少个匹配项。如果
$('#'+id).length
给您
0
,那么您知道没有匹配项。我将以字符串形式传递true/false(在asyncpage.php的页面中输出),但我假设运算符“==”会将每一个等于false的值传递为0”、“null”等。无论如何,我将尝试:If(result=='true'))谢谢你的建议:)好的,那不是问题所在。使用一个简单的警报,我检查了进程是否输入了if,但没有执行jquery命令。此时id的长度为1。id将始终是一个数字(与数据库中记录的id相对应),它是在表中创建的表的id。请在问题中而不是答案中发布其他信息。我会转达这个信息。请看我答案中的更新。