Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何自动填写表格_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 如何自动填写表格

Javascript 如何自动填写表格,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我想创建一个示例应用程序,当执行该应用程序时,它将自动选中“当前页面”复选框(id在脚本中硬编码) 我尝试了下面的代码,但什么都没有发生(复选框仍然未选中) test.html <!DOCTYPE html> <html> <head> <script type="text/javascript" src="test.js"></script> </head> <body> <input

我想创建一个示例应用程序,当执行该应用程序时,它将自动选中“当前页面”复选框(id在脚本中硬编码)

我尝试了下面的代码,但什么都没有发生(复选框仍然未选中)

test.html

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="test.js"></script>
</head>

<body>
    <input type = "button" onclick="check"/>
</body>
</html>
这个代码有什么问题,请帮帮我

编辑

“硬编码id”是来自“www.hello.com”等网站的复选框。当该网站打开时,以及当我的应用程序启动时,我希望复选框将被选中

我将以不同的方式问我的问题,是否可以创建一个应用程序扩展,在加载google站点时自动填充google搜索文本框。

尝试以下方法:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="test.js"></script>
    </head>

    <body>
        <input type = "button" onclick="check()"/>
    </body>
 </html>

无论如何,这是创建复选框的正确方法(不需要选中属性):


是的,可以选中某个页面上的复选框(或者填写文本框)。您需要做的只是在清单的
内容\u脚本
部分提到您的扩展脚本,并为其提供正确的匹配项

范例

"content_scripts": [ {
   "js": [ "test.js" ],
   "matches": [ "http://www.hello.com/*" ]
} ],
您的
test.js
将被注入页面,因此它将在扩展的范围外和网页的范围内运行。使用类似
window.onload
$(document.ready()
的机制,如果使用jQuery,您可以让脚本等待目标页面完成加载

$(document).ready(function() {
   check();
});

function check()
{
   document.getElementById('hard_coded_id').checked = true;
}

考虑到目标页面上存在
硬编码的\u id

输入没有id,因为他不想禁用按钮。我认为:之类的代码是故意删除的。没有“硬编码的\u id”是来自其他网站的test.html,用于测试您是否正确操作,或者实际上是扩展的一部分?您可以发布清单吗?我的问题不同,是否可以创建一个应用程序扩展,在加载google站点时自动填充google搜索文本框。我不明白您是如何做到的nt选中其他页面上的复选框?来自test.html?类似于自动填充应用程序。当网站打开时,应用程序会自动填充此页面的表单。我应该删除test.html文件吗?还是在项目中必须删除?
"content_scripts": [ {
   "js": [ "test.js" ],
   "matches": [ "http://www.hello.com/*" ]
} ],
$(document).ready(function() {
   check();
});

function check()
{
   document.getElementById('hard_coded_id').checked = true;
}