如何使用 Greasemonkey 自动选择单选按钮?

发布于 2025-01-03 01:51:58 字数 1286 浏览 1 评论 0原文

我想用 Greasemonkey 选择自动单选按钮。但我想选择带有标签名称的收音机。 当我看到这四个单选按钮时,Greasemonkey 需要选择我的选项。 示例:我想要自动选择标签 Instagram

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

如何使用 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>


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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖微凉心微凉 2025-01-10 01:51:58

试试这个:

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

如果不知道 HTML 是如何组合在一起的,我无法确定这是否有效。您在问题中给出了 HTML,但我不知道它是否保留了该恒定格式。

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.

暮年慕年 2025-01-10 01:51:58

我有一段时间没有使用 xpath,所以它可能不会做任何事情:)

但理论上,这将搜索文本包含“Instagram”的所有标签,然后循环遍历所有结果,将 for 属性设置为获取单选按钮并将其选中为 true。 (虽然我认为它是单选按钮的“选择”?)

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

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;
}
手心的海 2025-01-10 01:51:58
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
    }
}
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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文