2

私はGreasemonkey/Tampermonkeyスクリプトを書いていますが、ラジコンの名前と値に応じてラジオボタンをオンにする必要があります。

これはそれがどのように見えるかです:

<input type="radio" name="11" value="XXX" class="radio">
<input type="radio" name="11" value="zzzz" class="radio">

そこで、名前が11で値がzzzzのボタンをチェックするように設定します。

4

1 に答える 1

2

jQueryを使用すると、これは非常に簡単です。完全なスクリプトは次のとおりです。

// ==UserScript==
// @name     _Radio button check
// @include  http://YOUR_SITE/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
$("input[name='11'][value='zzzz']").prop ('checked', true);

input[name='11'][value='zzzz']jQueryセレクターは、初期<input>を持つすべてのを取得しますが、それらが。を含むラジオボタンのグループにある場合に限ります。zzzzname="11"

参照:


重要:   上記はほとんどのページで機能しますが、AJAX駆動型のページでは、多くの場合、 AJAX対応の手法を使用する必要があります。 これは、気になるチェックボックスが読み込まれる前に、Greasemonkeyスクリプトが実行されるためです。

これに対処する1つの方法はwaitForKeyElementsユーティリティを使用することです。そのようです:

// ==UserScript==
// @name     _Radio button check
// @include  http://YOUR_SITE/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("input[name='11'][value='zzzz']", selectRadioButton, true);

function selectRadioButton (jNode) {
    jNode.prop ('checked', true);
}



質問がされたときに「最先端」だった古い答え:):

// ==UserScript==
// @name            _Radio button check
// @include         http://YOUR_SITE/YOUR_PATH/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==

$("input[name='11'][value='zzzz']").attr ("checked", "checked");
于 2011-06-30T21:01:46.460 に答える