gpt4 book ai didi

javascript - Greasemonkey 更改表单中单选按钮的值?

转载 作者:行者123 更新时间:2023-11-29 22:32:12 24 4
gpt4 key购买 nike

我正在编写 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 选择器获取所有 <input>具有初始zzzz的s , 但前提是它们在带有 name="11" 的单选按钮组中.

引用:


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

处理此问题的一种方法是使用 the waitForKeyElements utility .像这样:

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

关于javascript - Greasemonkey 更改表单中单选按钮的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com