我正在尝试构建一个小书签,以使用文本属性 name="param[random-number][details]"
自动填充文本区域。
random-number
是在服务器上生成的,每次都不同。页面 HTML 如下所示:
<table>
<tbody>
<tr>
<td>Video Title:</td>
<td>
<input type="text" name="param[30301754][title]" value="VIDEO TITLE" autocomplete="on">
</td>
</tr>
<tr>
<td>File name:</td>
<td>
<input type="text" name="param[30301754][filename]" value="FILE NAME" autocomplete="on">.3gp
</td>
</tr>
<tr>
<td>Description: </td>
<td><textarea name="param[30301754][description]"></textarea>
</td>
</tr>
</tbody></table>
最佳答案
假设只有一个元素,如 <textarea name="param[xxx][description]">
在页面上,您可以使用 jQuery wildcards作为选择器。
$("textarea[name$=\\[description\\]]").val("test");
更新
要将其作为一个简单的小书签来执行,您需要提示用户输入他们想要自动填充的文本(或从其他地方获取),并将整个内容包装在一个匿名函数中。您还需要对 jQuery 库的引用。 (有关如何创建小书签的更多信息,请参阅 this article。)
这应该适合您的情况:
javascript: (function () {
if (!($ = window.jQuery)) {
script = document.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js';
script.onload = doAutoFill;
document.body.appendChild(script);
} else {
doAutoFill();
}
function doAutoFill() {
var textToFill = prompt("Enter text to fill:");
if (textToFill != null) $("textarea[name$=\\[description\\]]").val(textToFill);
}
})();
这是一个更新的 fiddle 示例:jsfiddle
关于javascript - 使用随机元素名称自动填充文本区域的书签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29780338/