如何更改 AJAX 驱动的页面中<输入>的名称字段

How to change the name field of an <input> in an AJAX-driven page?

本文关键字:输入 字段 AJAX 何更改      更新时间:2023-09-26

对于此目标页面(抱歉,SO不允许超链接到62.0.54.118):

http://62.0.54.118/search?&q=42&oq=42&sourceid=chrome&ie=UTF-8&filter=0

,我想默认使用用户脚本更改<input>的名称字段。

输入为:

<input class="gsfi" id="lst-ib" name="q" maxlength="2048" value="42"...>

我想将其更改为:

<input class="gsfi" id="lst-ib" name="&q" maxlength="2048" value="42"...>


也就是说,我想默认在输入的name字段中将q更改为&q

我尝试编写一个脚本(这不起作用):

// ==UserScript==
// @name     Normal Google Input
// @include  http://62.0.54.118/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/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*='q']", changeLinkQuery);
function changeLinkQuery (jNode) {
    var oldName = jNode.attr ('name');
    var newName = oldName.replace (/'q/, "&q");
    jNode.attr ('name', newName);
    return true;
}


此外,该页面是 Ajax 驱动的页面。

请修复我的坏脚本或帮助我编写另一个脚本,谢谢。

更新:我通过将脚本的一行从:

var newName = oldName.replace (/'q/, "&q");

var newName = oldName.replace (/q/, "&q");

然后我的脚本效果更好。感谢@Gerard塞克斯顿的建议。

但是现在有一个新的错误,waitForKeyElements回调设置为页面的 AJAX return true;,它会不间断地添加&

它导致此字段name="&&&&&&&&&q",等等。

我该如何解决它?

如果输入名称本身只是q,则只需将waitForKeyElements调用更改为:

waitForKeyElements ("input[name='q']", changeLinkQuery);

这样它就会准确地寻找q,而不是字符串中任意位置的q。


如果这不是该<input>的名称,请在下面评论。