2016-03-05 150 views
0

我有一個刪除按鈕,我在我的網站的許多領域全球使用。這是它的代碼。停止按鈕從提交然後使用JavaScript提交表單

public function delete_button($id_to_be_deleted, $form_to_post_to, $button_name){ 
    return form_open($form_to_post_to, array('class'=>'table_form spanFormat')) . 
    form_hidden(array('id'=>$id_to_be_deleted)) . 
    form_submit(array('class'=>'btn btn-sm btn-danger','name'=>$button_name, 'value'=>'Delete', 'onclick'=>'confirmDelete();return false;')) . 
    form_close(); 
} 

這段代碼,'onclick'=>'confirmDelete();return false;'就是應該暫時停止提交表單,這裏的運行我的功能,

function confirmDelete() { 
    var d = confirm("Are you sure you want to delete this?"); 
    if (d) return true; 
    else return false; 
} 

,如果屬實,則提交表單。

出於某種原因,它打開彈出窗口,但不提交表單之後,因爲返回false已經被解僱。但是,如果我刪除返回false,那麼表單將提交併永遠不會彈出打開彈出窗口。

有人可以幫我嗎?

注意:如果可能的話,我希望這樣做。我不想要一個表單提交,我希望我的按鈕處理程序。這是因爲我的刪除表單都有不同的名稱,但我的按鈕都應該彈出此確認對話框。

+0

''的onclick => 'return confirmDelete();'' –

+0

哈哈,我真的很笨。謝啦。難怪'confirmDelete()'不起作用。 – Nic

回答

0

Onclick是使用錯誤的事件。你要掛鉤提交事件,所以let's開始

<input type='submit' onclick=" 

那麼你一定要實際使用的確認結果,所以直接返回它:

<input type='submit' onclick="return confirm('sure?')"/>