How can I auto-select a radio button with Greasemonkey? [duplicate]

本秂侑毒 提交于 2020-02-08 10:20:53

问题


I want select auto radio button with Greasemonkey. But I want select radio with label name. When I see that four radio button, Greasemonkey need select my option. Example: I want auto select label Instagram.

<label for="ContentPlaceHolder1_answer3">Instagram</label>

How can I select this radio button with Greasemonkey?

<div class="row">
              <input type="radio" tabindex="3" value="answer1" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer1"><label for="ContentPlaceHolder1_answer1">Twitpic</label></div>

<div class="row">
            <input type="radio" tabindex="4" value="answer2" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer2"><label for="ContentPlaceHolder1_answer2">Yfrog</label></div>

<div class="row">
            <input type="radio" tabindex="5" value="answer3" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer3"><label for="ContentPlaceHolder1_answer3">Instagram</label></div>

<div class="row">
            <input type="radio" tabindex="6" value="answer4" name="ctl00$ContentPlaceHolder1$1" id="ContentPlaceHolder1_answer4"><label for="ContentPlaceHolder1_answer4">Flickr</label></div>

 </div>



回答1:


var labels = document.getElementsByTagName('label'); //get the labels
for (var i = 0; i < labels.length; ++i) { //loop through the labels
    if (labels[i].textContent == "Instagram") { //check label text
        labels[i].click(); //if correct text, click the label
    }
}



回答2:


Try this:

var label="whatever"; //The label you want to look for, you may already have some such variable
var allLabels=document.getElementsByTagName("label"); //Get all labels
for (var i=0;i<allLabels.length;i++) { //Loop through labels
    if (allLabels[i].innerHTML.toLowerCase()==label) { //If a match is found...
        allLabels[i].parentNode.getElementsByTagName("input")[0].checked=true; //Check the parent element for inputs (radio buttons, for instance), and check the first one
    }
}

I can't tell for sure if that will work without knowing how the HTML is put together. You gave the HTML in your question, but I don't know if it retains that constant format.




回答3:


I've not used xpath in a while, so it might not do anything :)

But in theory, this will search for all labels where the text constains "Instagram", and then it loops through all the results, taking the for attribute to get the radio button and sets it's checked to true. (although i think its "selected" for radio buttons?)

function xpathSnapshot(expression,context) {
    var r=document.evaluate(expression,context,null,6,null),i=r.snapshotLength-1;
    this.iterate=function() {return r.snapshotItem(i--)}
}

var labels = xpathSnapshot("//label[contains(text(),\"Instagram\")]",document);

while (label = labels.iterate()) {
    document.getElementById(label.getAttribute("for")).checked = true;
}


来源:https://stackoverflow.com/questions/9169595/how-can-i-auto-select-a-radio-button-with-greasemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!