javascript - 自动单击单选按钮但首先检查句子(标签)?

标签 javascript html radio-button greasemonkey

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

<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>

radio :

<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>

最佳答案

您必须找到距离<lable> 的同级最近的单选按钮(或文本节点)你关心。

jQuery 使这种事情变得更容易,参见 the jQuery documentation , 特别是 the Selectors sectionthe Traversing section .

这里是一个完整的 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);


您也可以See the code in action at jsFiddle.

关于javascript - 自动单击单选按钮但首先检查句子(标签)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330948/

相关文章:

javascript - 防止单击时检查单选按钮

javascript - 如何比较所有嵌套子元素的长度?

javascript - 如何在 html 中使用 javascript 获取 iframe 作为字符串并将其显示在 div 标记中?

html - 垂直对齐 :middle on text not working

android - 如何在自定义对话框中添加 radiogroup onchecked 监听器

jsf - p :radioButton with render rule is never rendered

javascript - 无法在 JS 中添加 PHP

javascript - 用 chai 发送 POST 请求发送一个空体?

javascript - 为什么我的 JavaScript 代码会收到 "No ' Access-Control-Allow-Origin' header is present on the requested resource"错误,而 Postman 却没有?

html - 我们需要自己用 <canvas> 实现双缓冲吗?