2014-09-02 68 views
1

我使用purecss作爲簡單項目的基礎。我目前遇到的問題是我有一個提交按鈕,我將從這些字段中提取一些信息,點擊時我想運行一些JavaScript,並將用戶轉到另一頁。這是我正在嘗試沒有運氣:onClick和操作html

<div class="pure-controls"> 
     <button type="submit" class="pure-button pure-button-primary" onClick="saveInfo(this.form)" action="confirm.html">Submit</button>   
     <button type="submit" class="pure-button pure-button-primary">Search</button> 
    </div> 
+2

只有''

有元素一個動作屬性。 – j08691 2014-09-02 19:16:52

+0

好,我怎麼能得到按鈕來執行js和更改頁面呢? – erp 2014-09-02 19:19:13

+0

在saveInfo中,可以設置document.forms [0] .action =「confirm.html」(假設它是頁面上的第一個表單)。 真的,你應該創建一個監聽按鈕的事件監聽器,並刪除整個內聯的東西。 – briansol 2014-09-02 19:20:35

回答

0

爲簡單起見,你的按鈕ID,並拿出那個討厭的內聯JS。

<div class="pure-controls"> 
    <button type="submit" class="pure-button pure-button-primary" id="button1">Submit</button>   
    <button type="submit" class="pure-button pure-button-primary" id="button2">Search</button> 
</div> 

然後用你的腳本:

var el = document.getElementById("button1"); //cache the save button 
el.addEventListener("click", function() { 
    document.forms[0].submit(); //submit the form or save it etc 
    window.location.href = "confirm.html"; 
}, false); //event handler 

發送的表單數據到函數,然後用一個簡單的重定向,因爲只有形成元素具有行動性。然後只需添加點擊事件,然後保存你的表單(提交,ajax調用,無所謂),然後在回調中或者它是如何重定向客戶端window.location.href

+0

所以我把代碼(或類似的東西)的底部部分放在js標籤的某處? ive從來沒有真正做過web開發,所以不知道很多東西 – erp 2014-09-02 19:36:22

+0

您可以通過外部腳本運行它,或將其包裝在''標籤底部的腳本標籤中。如果您對代碼不太瞭解,請查看我使用的每種方法。這可能需要一些時間,但這是很好的做法。 – 2014-09-02 19:37:54

+0

因此,如果它位於''底部的js標記中,我只是將它稱爲任何東西,但實際上並不需要調用它?我熟悉事件監聽器(android/java swing)。只是檢查。 – erp 2014-09-02 19:46:01

0

你可以使代碼裏面保存信息後,saveInfo()將用戶重定向到confirm.html

window.location = "confirm.html"

+0

MDN說'document.location'是一個只讀屬性 – 2014-09-02 19:27:30

0

你可以有一個按鈕,按下它可能看起來像一個Javascript事件處理程序:

document.getElementById('buttonID').onclick=function(){ 
    //Do whatever processing you need 
    document.getElementById('formID').submit(); 
}; 

或者,你可以在jQuery的事件處理程序,其會看起來像:

$('#buttonID').on('click',function(){ 
    // Do whatever processing you need 
    $('#formID').submit(); 
}); 
+0

請不要建議jQuery,除非問題被標記爲這樣,或OP表示他們對它開放。 – 2014-09-02 19:27:56

0
<div class="pure-controls"> 
    <button type="submit" class="pure-button pure-button-primary" id="button1">Submit</button>   
    <button type="submit" class="pure-button pure-button-primary" id="button2">Search</button> 
</div> 

<script> 
     $(document).ready(function() { 
      $('#button1').click(function(){ 
      var form = $(this).parents('form'); 

      $.ajax({ 
       type: 'POST', 
       url: $(this).attr('action'), 
       data: form.serialize(), 
       dataType: 'json', 
       success: function(response) { 
         window.location.href = "http://www.page-2.com"; 
       } 
      }); 
      return false; 
      }); 
     }); 
</script> 
0

你的方式都和d Ø沒有提交類型.. 類型可以是單純的..按鈕點擊之後,你現在就可以做你所有的JavaScript的東西上用onclick =「JavaScript的:somefunction()」,

somefunction = function(){ 
Process javascript here 

document.formname.submit(); 

location.href = "new.html" 

}