0

我正在编写我的第一个 chrome 扩展,它具有以下功能。当用户右键单击图像并在上下文菜单中选择适当的字段时,会创建一个包含表单的弹出窗口。从上下文菜单调用以下函数。

   function new_window(){chrome.windows.create({
                                   url: "reddit.html",
                                   type: "popup",
                                   focused: true,
                                   width: 200,
                                   height:140})

“reddit.html”中包含的表单如下:

     <form id="post_on_forum" method="get">
         <input type="text" name="title" placeholder ="title"><br>
         <input type ="text" name="Category"placeholder ="Category" ></br>
         <input type="submit" value="Submit" id="submit">                   
     </form>

我的问题是:如何使用 Javascript 获取用户每次提交内容时输入的变量?我不能使用内联 javascript,因为 chrome 禁止这样做。我创建了submit.js一个

$(document).ready(function() {
    $('#Submit').on('click',(function() {
       foo($('#post_on_forum').val());
    }));

    console.log("triggered");
}); 

但它不起作用。当然,我已经在清单文件中包含了所有适当的文件。

4

2 回答 2

3

看这个例子http://jsfiddle.net/qt3ZB/

jQuery 有特殊的方法serialize可以从表单中获取所有值。

希望能帮助到你。

于 2013-07-31T08:33:29.893 回答
0

下面的代码将以这种格式序列化表单输入(controlName1=controlValue1&controlName2=controlValue2)

$(document).ready(function() {
  $('#Submit').on('click',(function() {
    var formElements = $('#post_on_forum').serialize();       
    //then use this formElements wherever you like
 });
}); 
于 2013-07-31T08:34:12.157 回答