2012-11-11 34 views
2

我需要幫助... 我想自動點擊一個單選按鈕...但還需要檢查的句子(標籤)... 如...()< -radio按鈕自動點擊單選按鈕但首先檢查句子(標籤)?

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

好吧,這code..click答案()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> 
+0

你真的應爲第二組使用不同的「名稱」,例如'group2';這將允許您通過簡單地將'[name = group2]'添加到您的過濾器來乾淨地分離兩組按鈕。檢查上面標籤的價值似乎很脆弱。 – brichins

回答

0

你必須找到對應的單選按鈕那是你最近的,跟隨<lable>(或文本節點)的兄弟關於。

jQuery使這種事情更容易,見the jQuery documentation,尤其是the Selectors sectionthe Traversing section

這裏的一個完整的Greasemonkey腳本,將選擇合適的按鈕,基於問題的新的HTML(與谷歌文檔):

// ==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.

+0

嗨,謝謝你的回答。 我試過在谷歌文檔中使用表單上的腳本。 [鏈接](https://docs.google.com/spreadsheet/viewform?formkey=dG1fdUU1bTR5R1l3dmtGS095QVYxZlE6MQ) 但是什麼也沒有發生...... 前面的腳本確實有效...但是這有我提到的問題... 現在我設置了以下內容: 'var questionLabel = $( 「label:contains('How much is 1 + 1')〜input [type = radio] [value = 2]」 ).first() ; questionLabel。prop('checked',true);' – Kokox

+0

** Google文檔沒有任何地方與問題代碼具有相同的結構!!! **將**確切的,真實的HTML **放入問題中,或鏈接到**真實**目標頁面。 ...另外,你需要我的答案中指定的'@ require'和'@ grant'指令。 –

+0

感謝您的耐心Broks, 鏈接是這樣的: https://docs.google.com/spreadsheet/viewform?formkey=dG1fdUU1bTR5R1l3dmtGS095QVYxZlE6MQ 我會編輯原始問題,以便您看得更有序。 – Kokox