Javascript Greasemonkey更改窗体中单选按钮的值?

Javascript Greasemonkey更改窗体中单选按钮的值?,javascript,forms,radio-button,greasemonkey,tampermonkey,Javascript,Forms,Radio Button,Greasemonkey,Tampermonkey,我正在编写Greasemonkey/Tampermonkey脚本,需要根据单选控件的名称和值打开单选按钮 这就是它的样子: <input type="radio" name="11" value="XXX" class="radio"> <input type="radio" name="11" value="zzzz" class="radio"> 所以我想设置那些名称为11,值为zzzz的按钮进行检查 使用jQuery时,这非常简单。以下是完整的脚本: // =

我正在编写Greasemonkey/Tampermonkey脚本,需要根据单选控件的名称和值打开单选按钮

这就是它的样子:

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


所以我想设置那些名称为11,值为zzzz的按钮进行检查

使用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选择器获取具有初始值
zzzz
的所有
,但前提是它们位于带有
name=“11”
的单选按钮组中

参考:


重要提示:以上内容适用于大多数页面,但在AJAX驱动的页面上,通常需要使用AJAX感知技术。这是因为Greasemonkey脚本将在加载您关心的复选框之前运行

解决这一问题的一种方法是解决问题。像这样:

// ==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");

如果它包括对正在发生的事情的简单解释和对一些文档的引用,这将更有帮助。特别是因为这是一个非常基本的问题。即使同时阅读了一些非常有帮助的教程,我相信谷歌上的其他人以前也不会使用jQuery。谢谢:)