0

Given the following HTML:

<table class="cat_list">
<tr>
    <td>
        <input type="checkbox" name="filter_cat[1]" id="cat_1" value="1"/>
        <label for="cat_1">Music</label>
    </td>
    <td>
        <input type="checkbox" name="filter_cat[2]" id="cat_2" value="1"/>
        <label for="cat_2">Applications</label>
    </td>
    <td>
        <input type="checkbox" name="filter_cat[3]" id="cat_3" value="1"/>
        <label for="cat_3">E-Books</label>
    </td>
    <td>
        <input type="checkbox" name="filter_cat[4]" id="cat_4" value="1"/>
        <label for="cat_4">Audiobooks</label>
    </td>
    <td>
        <input type="checkbox" name="filter_cat[5]" id="cat_5" value="1"/>
        <label for="cat_5">E-Learning Videos</label>
    </td>
    <td>
        <input type="checkbox" name="filter_cat[6]" id="cat_6" value="1"/>
        <label for="cat_6">Magazines</label>
    </td>
    <td>
        <input type="checkbox" name="filter_cat[7]" id="cat_7" value="1"/>
        <label for="cat_7">Comics</label>
    </td>
</tr>

The following script should uncheck all checkboxes in the table row, then check the one specific one with id cat_1.

$(".cat_list input[type='checkbox']").attr("checked",false);

$("#cat_1").attr("checked",true);

In Greasemonkey, it checks the one box for a millisecond then just unchecks all boxes. The second command alone DOES check the cat_1 box when executed alone, but when you add the line above it, they all get unchecked after...

So, is Greasemonkey executing things incorrectly or what? it looks like it's running the script backwards or there's a timing issue, like it's starting the unchecks async then finishing the check before the uncheck can finish.

4

2 に答える 2

1

スクリプトの jQuery とページが競合している可能性があります。簡単@grant GM_addStyleに修正できます。完全なスクリプトを投稿してください。


それ以外の場合、その jQuery コードは同期的に動作します。ページの JavaScript が、あなたがやろうとしていることを妨げているか、何か重要なことが質問から省略されています。

おそらく、ページの設定が完了する前にスクリプトが実行されている可能性があります。可能であれば、実際のターゲット ページにリンクします。

これらの入力に作用するページの JavaScript を分析し、ページ独自の JS を使用して変更を行うことを検討してください。ページでは、チェックボックスの切り替えだけでなく、状態の設定が必要または期待される場合があります。

スクリプト インジェクションまたは 経由でページの JS を使用しますunsafeWindow


psyjonizの回答のように、タイマーを使用するだけで十分かもしれません。ではないかもしれない。しかし、試してみるのは簡単です。ただし、より長い間隔を使用します。

$(".cat_list input[type='checkbox']").attr ("checked", false);

//-- Give the page's JS time to react and settle.
setTimeout ( function () { $("#cat_1").attr ("checked", true); }, 100);
于 2013-09-10T22:32:12.027 に答える
0

これを試して ..

setTimeout(function(){$("#cat_1").attr("checked",true);},0);

ある種の競合状態である場合、コマンドはスタックの一番下に置かれます。

于 2013-09-10T17:58:18.063 に答える