2

我需要帮助...我想自动单击一个单选按钮...但还需要检查句子(标签)...例如...() <-radio button

<label>How much is 1+1?</label><br/>
<input type="radio" name="group1" value="2"/>()2<br/>
<input type="radio" name="group1" value="3"/>()3<br/>
<input type="radio" name="group1" value="4"/>()4

我尝试使用这个脚本:

// ==UserScript==
// @name script
// @description Auto select rpt radio button
// @include *
// ==/UserScript==

if(radio=document.evaluate('//input[@type="radio" and @value="2"]',document,null,9,null).singleNodeValue)
radio.checked=true;

好的,这段代码..click answer ()2 但是如果我有以下情况会发生什么:

<label>How much is 1+1?</label><br/>
<input type="radio" name="group1" value="2"/>()2<br/>
<input type="radio" name="group1" value="3"/>()3<br/>
<input type="radio" name="group1" value="4"/>()4

How much is 4+2?<br/>
<input type="radio" name="group1" value="8"/>()8<br/>
<input type="radio" name="group1" value="2"/>()2<br/>
<input type="radio" name="group1" value="6"/>()6

什么都没有发生,因为有两个值叫“2”..所以我尝试修改代码先检查句子然后标记答案(但我没有很好地掌握它)...

if(label=document.evaluate('//label[@value="How much is 1+1?"]',document,null,9,null).singleNodeValue)&&(radio=document.evaluate('//input[@type="radio" and @value="2"]',document,null,9,null).singleNodeValue)
radio.checked=true;

我的意图是添加第二个条件,检查带有“1+1 是多少?”的值的标签。

谁能指导我如何做到这一点?

编辑:示例链接: Google docs form

页面代码如下所示: 标签:

<label class="ss-q-title" for="entry_0">How much is 1+1
<span class="ss-required-asterisk">*</span></label>

收音机:

<ul class="ss-choices"><li class="ss-choice-item"><label class="ss-choice-label"><input name="entry.0.group" value="2" class="ss-q-radio" id="group_0_1" type="radio">
2</label></li> <li class="ss-choice-item"><label class="ss-choice-label"><input name="entry.0.group" value="3" class="ss-q-radio" id="group_0_2" type="radio">
3</label></li> <li class="ss-choice-item"><label class="ss-choice-label"><input name="entry.0.group" value="4" class="ss-q-radio" id="group_0_3" type="radio">
4</label></li>
</ul>
4

1 回答 1

0

您必须找到最近的单选按钮,跟随<lable>您关心的(或文本节点)的兄弟。

jQuery 使这种事情变得更容易,请参阅jQuery 文档尤其Selectors部分Traversing部分

这是一个完整的 Greasemonkey 脚本,它将根据问题中的新 HTML(链接的 Google 文档)选择正确的按钮:

// ==UserScript==
// @name        YOUR_SCRIPT_NAME
// @description Auto select rpt radio button
// @include     http://YOUR_SERVER.COM/YOUR_PATH/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/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.
*/
//-- Note that :contains() is case-sensitive
var questionLabel   = $(
    "label.ss-q-title:contains('How much is 1+1') ~ ul.ss-choices"
).first ().find ("input[type=radio][value=2]");
questionLabel.prop ('checked', true);


您还可以在 jsFiddle 查看实际代码。

于 2012-11-11T13:11:26.320 回答