[英]How to submit values by double-clicking in drop-down list?
I'm looking for a way to submit a value with two clicks in drop-down list ( which is auto-sized ) using Greasemonkey. 我正在寻找一种使用Greasemonkey在下拉列表( 自动调整大小 )中单击两次以提交值的方法。
Target HTML: 目标HTML:
<form method="post">
<select size="3" name="z_pos_id">
<option value="2463">Option A</option>
<option value="2609">Option B</option>
<option value="3013">Option C</option>
</select>
<input type="submit" value="Button">
</form>
At the moment I click on the option to select it and then I click on the button to submit the value (Press Enter works too). 此刻,我单击选项以将其选中,然后单击按钮以提交值(按Enter也可以)。
But with a Greasemonkey script I want to click on the option to select it and click again on the selected option to submit it. 但是,对于Greasemonkey脚本,我想单击该选项以将其选中,然后再次单击所选的选项以将其提交。 Is this possible? 这可能吗?
One way to handle double left -clicks, without interfering with center and right-clicks (too much), is to: 处理双左 -clicks,而不中心和右键单击(太多)干扰的方法之一,就是:
<option>
was selected when the mousedown
event fires. 跟踪在mousedown
事件触发时是否选择了<option>
。 To simplify the code greatly, use jQuery this time. 为了大大简化代码,这次使用jQuery。 See the code in action at jsFiddle . 参见jsFiddle上的代码 。
A complete script would look something like this: 完整的脚本如下所示:
// ==UserScript==
// @name _Activate double-click on select, submits form
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/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.
*/
$("select[name=z_pos_id] option").bind ("mousedown click", handleOptionClicks);
function handleOptionClicks (zEvent) {
_self = handleOptionClicks;
_self.optionWasSelected = _self.optionWasSelected || false;
if (zEvent.type == "mousedown") {
_self.optionWasSelected = zEvent.target.selected;
}
else {//zEvent.type == "click"
if (zEvent.target.selected && _self.optionWasSelected) {
//-- Double-click, so submit form:
$(this).parents ("form").submit ();
}
}
}
Update: Here's a version that will work on Chrome as well (need to use script injection). 更新:这是一个也可以在Chrome上使用的版本(需要使用脚本注入)。
This assumes that the target page does not already have jQuery: 这假定目标页面尚不具有jQuery:
// ==UserScript==
// @name _Activate double-click on select, submits form
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant none
// ==/UserScript==
function GM_main ($) {
$("select[name=z_pos_id] option").bind ("mousedown click", handleOptionClicks);
function handleOptionClicks (zEvent) {
_self = handleOptionClicks;
_self.optionWasSelected = _self.optionWasSelected || false;
if (zEvent.type == "mousedown") {
_self.optionWasSelected = zEvent.target.selected;
}
else {//zEvent.type == "click"
if (zEvent.target.selected && _self.optionWasSelected) {
//-- Double-click, so submit form:
$(this).parents ("form").submit ();
}
}
}
}
add_jQuery (GM_main, "1.7.2");
function add_jQuery (callbackFn, jqVersion) {
jqVersion = jqVersion || "1.7.2";
var D = document;
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
var scriptNode = D.createElement ('script');
scriptNode.src = 'http://ajax.googleapis.com/ajax/libs/jquery/'
+ jqVersion
+ '/jquery.min.js'
;
scriptNode.addEventListener ("load", function () {
var scriptNode = D.createElement ("script");
scriptNode.textContent =
'var gm_jQuery = jQuery.noConflict (true);\n'
+ '(' + callbackFn.toString () + ')(gm_jQuery);'
;
targ.appendChild (scriptNode);
}, false);
targ.appendChild (scriptNode);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.