可以选择单选按钮的lublessmonkey脚本 [英] greasemonkey script to select radio button

查看:67
本文介绍了可以选择单选按钮的lublessmonkey脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.我有一个与油猴有关的问题.

i'm new here. i've a question related to greasemonkey.

一个页面包含多个单选按钮值,并且要做出一个选择,该正确的选择选项隐藏在页面中

A page contain multiple radio buttoned values and one choice is to made, this correct choice option is hidden within the page

单选按钮以结构为

<form name="bloogs" action="/myaddres.php" id="bloogs" method="post" >

然后将隐藏字段显示为

<input type=hidden value="abcd" name="ans">

然后所有单选按钮的值都按

then all the radio button values are followed as

<input  type="radio" id="r1" name="opt" value="abcd"> abcd
<input  type="radio" id="r2" name="opt" value="efgh"> efgh
<input  type="radio" id="r3" name="opt" value="ijkl"> ijkl

以此类推

因此,我需要在页面加载后立即检查"具有value = abcd的按钮.谢谢

thus i need the button with value=abcd be 'checked' as soon as the page loads. Thanks

推荐答案

有几种使用方法:

1 您可以这样输入selected="selected"来预先选择它:

1 You can pre-select it by putting in selected="selected" like this:

<input type="radio" id="r1" name="opt" value="abcd" checked="checked" /> abcd

2 您可以使用 jQuery 轻松进行操作(不过,我不知道这是否适用于greasmonky)

2 You can use jQuery to do it easily (I don't know whether it will be applicable in terms of greasmonky though)

$(function(){
  $('input[value="abcd"]').attr('checked', 'checked');
});

3 您可以遍历所有元素并使用原始javascript选择一个元素

3 You can loop through all elements and selected the one with raw javascript

var form = document.getElementById('bloogs');

for(var i=0; i<form.elements.length; i++)
{
  if (form.elements[i].type == 'radio')
  {
    if (form.elements[i].value == 'abcd')
    {
      form.elements[i].setAttribute('checked', 'checked');
      break;
    }
  }
}

更新:

这将使用jquery并从隐藏字段中读取值后选择一个单选:

This uses jquery and selects a radio after reading the value from hidden field:

$(function(){
  $('input[value="' + $('#hidden_field_id').val() + '"]').attr('checked', 'checked');
});

或使用原始javascript:

Or with raw javascript:

var form = document.getElementById('bloogs');
var hidden_value = document.getElementById('hidden_field_id').value;

for(var i=0; i<form.elements.length; i++)
{
  if (form.elements[i].type == 'radio')
  {
    if (form.elements[i].value == hidden_value)
    {
      form.elements[i].setAttribute('checked', 'checked');
      break;
    }
  }
}

更新2:

按照名称,这是您可以使用的方法:

As per the name, here is how you can go about:

$(function(){
  $('input[value="' + $('input[name="ans"]').val() + '"]').attr('checked', 'checked');
});

这篇关于可以选择单选按钮的lublessmonkey脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆