使用Greasemonkey添加表单标签

时间:2013-03-18 20:35:48

标签: javascript forms greasemonkey labels

供应商的网络应用程序要求我选择一个广播组中的缩略图,然后点击保存,我必须这样做超过300次。他们没有礼貌地使单选按钮内容可以点击。我以为通用汽车会允许我这样做,但我对如何编写脚本感到茫然。如果我可以让单选按钮继续并按下提交按钮,那就更好了。这是代码:

<div id="image1" class="image_selection">
<input type="radio" id="" class="" name="selectimageid" value="1"/>
<img id="1" class="thumb" src="/images/thumb1.jpg" /> 
</div>

<div id="image2" class="image_selection">
<input type="radio" id="" class="" name="selectimageid" value="2"/>
<img id="2" class="thumb" src="/images/thumb2.jpg" /> 
</div>

<input type="submit" id="" class="save_button" name="" value="" />

有人能指出我正确的方向吗?谢谢!

1 个答案:

答案 0 :(得分:1)

要添加标签,请使用jQueryjQuery's .wrap() function。这样的事情应该这样做:

$("input[name='selectimageid']").wrap ('<label></label>');

但是,我假设您希望缩略图成为标签的可点击部分。一个完整的Greasemonkey脚本是:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/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.
*/

var imgRadioBtns = $("input[name='selectimageid']");

imgRadioBtns.each ( function () {
    var radBtn  = $(this);
    var toWrap  = radBtn.nextAll ("img.thumb").addBack ();

    toWrap.wrapAll ('<label></label>');
} );