2016-08-25 75 views
0

我有一個帶有多個提交按鈕的表單,然後使用$ _POST信息進行處理。當按下回車鍵時的默認行爲當然是頁面上的第一個按鈕被「點擊」,但是我希望點擊的按鈕取決於用戶在單擊它時在表單中的位置。下面顯示了一段php代碼的摘錄摘錄。請注意,我已經嘗試使用onkeydown/onkeypress/onkeyupp和使用事件keycode(它等於13)然後document.getElementById(「buttonName」)。click()但這似乎並不工作 - 第一個按鈕形式仍然是一個點擊。按下帶有多個提交按鈕的回車鍵

if (isset($_POST['submit'])) { 
    // move to next page 
} 

if (isset($_POST['add'])) { 
    // add item to this page 
} 

$html .= '<input type="text" class="text" name="submitInfo" id="submitInfo">'; // if enter is clicked while in this textbox, the submit button should be clicked 

$html .= '<input type="text" class="text" name="OptionalAddInfo" id="OptionalAddInfo">'; // if enter is clicked while in this textbox, the add button should be clicked 

$html .= '<input type="submit" class="button" name="add" id="add" value="Add" onClick="return addItem();">'; 

$html .= '<input class="button" type="submit" name="submit" id="submit" value="Submit" onClick="return submitForm();">'; 

echo $html; 
+1

你爲什麼不使用兩種形式?或者關於你使用'onkeydown'來測試文本字段中的鍵13的嘗試,你是否也取消了keydown事件的默認行爲? – nnnnnn

+0

請添加您的JavaScript代碼以及 –

+0

是的,我也取消了JavaScript的默認行爲,然後告訴它單擊另一個按鈕。 多種形式可能最終成爲我不得不選擇的選項,但由於幾個原因,它不是我的首選。首先,所有這些信息都是針對這個「形式」的,因爲我試圖暗示有必要提交信息,但也有一些可以添加的可選信息(這需要根據用戶輸入提取一些信息)。所有這些都會被提交。所以所有的信息真的屬於一起。佈局也使多種形式不理想。 – John

回答

1

使用jQuery.Hotkeys一起工作回車鍵hotkeys

jQuery(document).bind('keydown', 'return', function() { 
        $("body #submit").trigger("click"); 
        return false; 
       }); 
相關問題