【问题标题】:Greasemonkey to change value of Radio buttons in a form?Greasemonkey 更改表单中单选按钮的值?
【发布时间】:2011-09-26 05:27:18
【问题描述】:

我正在编写 Greasemonkey/Tampermonkey 脚本,我需要根据单选控件的名称和值打开单选按钮。

看起来是这样的:

<input type="radio" name="11" value="XXX" class="radio">
<input type="radio" name="11" value="zzzz" class="radio">

所以我想设置那些名称为 11 且值为 zzzz 的按钮进行检查。

【问题讨论】:

    标签: javascript forms radio-button greasemonkey tampermonkey


    【解决方案1】:

    当您使用 jQuery 时,这非常容易。这是一个完整的脚本:

    // ==UserScript==
    // @name     _Radio button check
    // @include  http://YOUR_SITE/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/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.
    */
    $("input[name='11'][value='zzzz']").prop ('checked', true);
    

    input[name='11'][value='zzzz'] jQuery 选择器获取所有具有 initialzzzz&lt;input&gt;s,但前提是它们位于带有 name="11" 的单选按钮组中。

    参考:


    重要提示: 上述内容适用于大多数页面,但在 AJAX 驱动的页面上,您通常需要使用 AJAX 感知技术。这是因为 Greasemonkey 脚本将在您关心的复选框加载之前运行。

    处理此问题的一种方法是使用the waitForKeyElements utility。像这样:

    // ==UserScript==
    // @name     _Radio button check
    // @include  http://YOUR_SITE/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.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.
    */
    waitForKeyElements ("input[name='11'][value='zzzz']", selectRadioButton, true);
    
    function selectRadioButton (jNode) {
        jNode.prop ('checked', true);
    }
    



    旧答案是“最先进的”:) 当问题被问到时:

    // ==UserScript==
    // @name            _Radio button check
    // @include         http://YOUR_SITE/YOUR_PATH/*
    // @require         http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
    // ==/UserScript==
    
    $("input[name='11'][value='zzzz']").attr ("checked", "checked");
    

    【讨论】:

    • 如果它包括对正在发生的事情的简单解释和对一些文档的引用,这将更有帮助。特别是因为这是一个如此基本的问题。
    • 即使同时阅读了一些非常有帮助的教程,我敢肯定其他人在谷歌上搜索这个之前也不会使用 jQuery。谢谢:)
    猜你喜欢
    • 2019-06-05
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 2016-07-26
    相关资源
    最近更新 更多