2013-07-31 24 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 
}); 
});