当您使用 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>
具有初始值的s zzzz
,但前提是它们位于带有 . 的单选按钮组中name="11"
。
参考:
重要提示: 以上内容适用于大多数页面,但在 AJAX 驱动的页面上,您通常需要使用 AJAX 感知技术。 这是因为 Greasemonkey 脚本将在您关心的复选框加载之前运行。
解决此问题的一种方法是使用实用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");