2013-07-31 22 views
0

我正在写我的第一个Chrome扩展,它具有以下功能。当用户右键单击图像并在上下文菜单中选择适当的字段时,会创建一个包含表单的弹出窗口。从上下文菜单中调用以下函数。如何通过javascript在Google Chrome上通过html表单提交用户提交的值?

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"); 
}); 

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

+0

你在控制台得到一个错误序列化表单输入? – davy

+0

none我没有得到任何东西 – user1608778

回答

2

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

jQuery有特殊的方法serialize从形式获取所有值。

希望它有帮助。

+0

谢谢,我的问题是,似乎Chrome并没有“注册”点击提交按钮。当我在我的服务器上而不是扩展它的工作正常 – user1608778

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 
}); 
});