0

データベースから情報を削除する非常に複雑なコード (JQuery および HTML5 Web SQL) があります。ただし、データベースの削除は正常に機能しますが、その間に情報をビューから消したいと思います (ページが更新され、データベースが再度クエリされるまでは表示されません)。

セレクターを正しくするだけの問題だと確信しています

現在、私は持っています

$('.remove_event').live('click', function() {
    //TRIGGERS SQL REMOVE
    mbffdb.webdb.deleteevent($(this).attr('id')),
    //MAKES INFO DISSAPPEAR
    $(this).attr('id').addClass('displaynone');
});//END CLICK

情報は、動的 ID が与えられた div に含まれてい$(this).attr('id')ます (ID は、削除する SQL ステートメントで使用されます)。

誰かが私を助けることができれば、それは本当に素晴らしいことです!

4

3 に答える 3

2

問題は、文字列にクラス...を追加することです。フォローしてください:

$(this) 

要素です

$(this).attr("id") 

属性「id」の値です。つまり、クラスを追加できないようにするための文字列です。

ちなみに、IDとクラスのどちらを追加しますか?それははっきりしていません。IDを追加する場合は、を使用します.attr("id","mynewid")。クラスを追加する場合は、$(this).addClass("class")


編集:JQueryの連鎖性に誤解されている可能性があります。つまり、JQUery要素または要素のセットを操作する多くの関数が別の要素または要素のセットを返します。

例えば:

$("div.mydiv").parent().next().find("p").removeClass("otherclass");
              ^        ^      ^         ^

すべての呼び出し(^)で、関数は他の要素を返すため、別の関数をアタッチしてそれらの要素を操作したり、より多くの要素を生成したりできます。ただし、これは次のような一部の関数には当てはまりません。

.val() .text() .html()

要素ではなく「純粋な」値を返すため、その後に、addClass()con要素を操作するような関数を使用しても意味がありません。

于 2012-06-25T01:20:35.907 に答える
2

$(this).attr('id').addClass('displaynone');これに変更$(this).addClass('displaynone');

于 2012-06-25T01:20:54.317 に答える
0
$('.remove_event').live('click', function() {
    // $(this) refers to the element that was clicked.
    // $(this).attr('id') will return the attribute "id" of the element

    // TRIGGERS SQL REMOVE
    // the line below will execute the "deleteevent" function
    // passing the attribute id of $(this) as a parameter.
    mbffdb.webdb.deleteevent($(this).attr('id')),

    // MAKES INFO DISSAPPEAR
    // the line below will not work because you're trying to add a class 
    // to a non-element. ($(this).attr('id') returns the attribute! 
    // you can't chain!)
    // $(this).attr('id').addClass('displaynone');

    // Instead you should use this line 
    // which will add a class "displaynone" to the element $(this)
    $(this).addClass('displaynone');
    // or you can also set 'style="display:none;"' by doing
    $(this).css('display', 'none');
});//END CLICK
于 2012-06-25T01:32:34.720 に答える