JavaScript(Greasekit)单击单选按钮框并提交表单

时间:2013-06-28 00:48:57

标签: javascript radio-button greasekit

由此:

<div class="form-radio form-component">
    <fieldset>
        <legend>Where would you like your ad to appear?<span class="error"></span>
        </legend>
        <div class="form-radio-options">
            <div class="radio-option">
                <input type="radio" id="bump-up-ad_bumpUp_1" name="bumpUp" value="false" />
                <label for="bump-up-ad_bumpUp_1">FREE - Keep it where it is</label>
            </div>
            <div class="radio-option">
                <input type="radio" id="bump-up-ad_bumpUp_2" name="bumpUp" value="true" />
                <label for="bump-up-ad_bumpUp_2">&pound;1.53 - Bump my ad to the top of the listings</label>
            </div>
        </div>
    </fieldset>
</div>

我只是想让它自动选择与“FREE - 保持原样”相对应的单选按钮,然后我希望它单击此按钮发送表单:

<div class="form-component form-submit">
    <input id="bump-up-ad_submit-new-advert-data" name="confirm" type="submit" class="button" value="Update my ad">
</div>
<div class="form-component form-submit">

1 个答案:

答案 0 :(得分:1)

document.getElementById('bump-up-ad_bumpUp_1').checked = true;
document.getElementById('bump-up-ad_submit-new-advert-data').click();

如果您的浏览器不支持.click(),我们必须模拟点击:

function fireEvent(obj, evt) {
    var fireOnThis = obj;
    if( document.createEvent ) {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent( evt, true, false );
        fireOnThis.dispatchEvent(evObj);
    } else if( document.createEventObject ) {
        fireOnThis.fireEvent('on'+evt);
    }
}
document.getElementById('bump-up-ad_bumpUp_1').checked = true;
fireEvent(document.getElementById('bump-up-ad_submit-new-advert-data'), 'click');