2009-07-31 107 views
1

我可以在單選按鈕的onclick事件中定義一些Ruby功能(JavaScript除外)嗎?我可以爲onclick事件定義一個Ruby函數嗎?

例如,我可以使用類似的東西嗎?

<%= radio_button(count, voting.vote_count, :onclick => "if voting.nominees.eql?(selected.nominees) voting.update_attribute('vote_count',voting.vote_count+1)") 
+0

我們可以用`javascript-misunderstanding`開始標記這樣的問題嗎?最近有很多...... O_O – deceze 2009-07-31 12:57:52

回答

0

這是不可能的,沒有Javascript,即使它會阻止用戶運行任何她想要的任意代碼?

2

在這種情況下,Ruby正在服務器上運行,並且JavaScript正在瀏覽器上運行。

當點擊發生時,Ruby程序已經完成運行。

獲取Ruby函數運行的唯一方法是向服務器發出一個新的HTTP請求。 XMLHttpRequest對象可讓您從JavaScript執行此操作。爲了在沒有JavaScript的情況下實現它,你需要做一些假設。

<form action="..." method="post"> 
    <div> 
    <button type="submit"> 
     <img src="looks-like-a-radio-button.png" alt="vote"> 
    </button> 
    <input type="hidden" name="vote" value="whatever"> 
    </div> 
</form> 
+0

我可以在沒有onclick事件的情況下增加vote_count。 其實我想在用戶選擇答案時增加vote_count – sts 2009-07-31 13:10:00

相關問題