jQuery - 打开一个 URL 并选择一个值

jQuery - Open a URL and select a value

本文关键字:一个 选择 URL jQuery      更新时间:2023-09-26

使用GreaseMonkey,希望选择一个值并在页面加载后点击提交按钮。让我们以 False/No 为例

window.addEventListener ("load", LocalMain, false);
function LocalMain () {
  
$('select[name=timeEnabled]').val(1); 
$('input[type=submit]')[0].click(); 
}
  <select name="timeEnabled" onchange="timeUpdated()" class="alignLeft nowrap">
  <option value="true" selected="selected">Yes</option>
  <option value="false">No</option></select>
<input name="submit" value="Apply" onclick="return preSubmit()" class="btn btn-small btn-primary" type="submit">

对于GreaseMonkey,我怀疑这是有效的。下面是一个 jsFiddle 示例:https://jsfiddle.net/Twisty/xtmpca4b/

.HTML

<form>
<select name="timeEnabled" onchange="timeUpdated()" class="alignLeft nowrap">
  <option value="true" selected="selected">Yes</option>
  <option value="false">No</option>
</select>
<input name="submit" value="Apply" onclick="return preSubmit()" class="btn btn-small btn-primary" type="submit">
</form>

油脂猴

// ==UserScript==
// @name        Apply Each Tab
// @date                01/27/2016
// @require     https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @namespace   http://www.your-url-target.com/page-target.html
// @description Select a value from an element and submit the parent form.
// @version     0.1
// @grant       none
// ==/UserScript==
$(document).ready(function() {
  $("select[name='timeEnabled']").val("false");
  $("input[type='submit']")[0].click();
});

请务必更新@namespace以匹配目标网址。使用该.ready()将导致在加载页面时执行 JQuery。

您可能还需要考虑:

$("select[name='timeEnabled']").val("false").parent("form").submit();

示例:https://jsfiddle.net/Twisty/xtmpca4b/3/

是的,您可以使用 .val(optionIndex) 手动选择一个值。然后,您可以通过.click()手动单击应用按钮。

$('select[name=timeEnabled]').val(1); // Select the 'No' option for example. 
$('input[type=submit]')[0].click(); // Click the button, use [0] to use the native JS function instead of the jQuery one (which is used for handling onclick events).

听起来您正在寻找的是一个浏览器插件,它将在页面加载时执行脚本。查看油脂猴 https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/

在 Firefox 中安装该插件,编写您的 JavaScript 以操作相关的表单元素并提交表单(在此处查看他们的文档)。然后打开所有编辑链接。