开发者

Using jquery remove() with id passed by variable. Possible?

开发者 https://www.devze.com 2023-01-12 20:43 出处:网络
This is the function function deleteItem(id) { $.post(\"_asyncpage.php\", {id:id}, function (result) { if (result == true) {

This is the function

function deleteItem(id) {
    $.post("_asyncpage.php",
        {id:id},
        function (result) {
            if (result == true) {
                $('#'+id).remove();
            }
        }, "json"); 
}

So, to explain, the function receive an id, send to a page that execute random stuff on a db and return true/false.

The function ins开发者_如何学Cide check for result that can be true/false as said before. If true proceed to remove the dom element that match the id passed.

The db is updated correctly but the .remove() won't work... someone knows why? :(

The following is an example of the html structure. the table inside the TD is the one to be deleted.

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


Update: From the additional info you provided, the issue may be with your IDs. It is not valid for an ID to start with a number. This can cause problems.


If your id that you're passing to the function already has a # at the beginning, then you don't want to concatenate it.

Also, if the response you're getting is a string, then you would want to compare the result to the string 'true'.

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


Is the $('#'+id).remove(); getting executed?

What happens if you do alert($('#'+id).length)? You should expect an answer > 0

Also, what is the type of result? Try alert(typeof(result)); This will help you determine the correct comparison for your if check


I would inspect your result. Do you return true as number (1)? or is it a string when received?

Since you're dataType is json, the value of result must be a json string or an already parsed object, so I bet pretty much that this

if (result == true) {

will never pass.


Just to give more info, this is a portion of the structure that need to be removed via 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消