2016-08-24 52 views
2

試圖從JQuery中觸發提交功能。我究竟做錯了什麼?理論上,這應該起作用。儘管我嘗試過4種不同的方式。強制表單提交按鈕點擊jquery

我已經試過

$('input#submit').trigger("click"); 

$(form:first).submit(); 

$(form:first).trigger("submit"); 

$('form#databaseActionForm').submit(); 

NOTHNG曾先後(YET)?

CODE:

<html lang="en"> 
<head> 
    <title>Database Management</title> 
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
     <script src="http://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script> 
     <style> 
      table td { border: 1px solid black; } 
      table td:first-child { text-align: left; } 
     </style> 
    <script> 
     <!-- 
     $(document).ready(function() { 
      $('#erase').click(function() { 
       if (confirm("Are you sure that you want to ERASE ALL DATA from the database?")) 
       { 
        $('#buttonTypePressed').val("erase"); 
        $('input#submit').trigger("click"); <!-- HERE --> 
       } 
      }); 

      $('#update').click(function() { 
       var appID = $('#updateAppID').val(); 
       var field = $('#fieldName').val(); 
       var value = $('#newValue').val(); 

       if (appID == null || appID == "") { 
        alert("You must enter the ID number of the entry you wish to modify."); 
        $('#updateAppID').focus(); 
       } 
       else if (field == null || field == "") { 
        alert("You must choose which field you wish to modify."); 
        $('#fieldName').focus(); 
       } 
       else if (value == null || value == "") { 
        alert("You must choose the new value you wish to appear in the database."); 
        $('#newValue').focus(); 
       } 
       else { 
        $('#buttonTypePressed').val("update"); 
        $('input#submit').trigger("click"); <!-- HERE --> 
       } 
      }); 

      $('#delete').click(function() { 
       var appID = $('#deleteAppID').val(); 

       if (appID == null || appID == "") { 
        alert("You must enter the ID number of the entry you wish to delete."); 
        $('#deleteAppID').focus(); 
       } 
       else { 
        $('#buttonTypePressed').val("delete"); 
        $('input#submit').trigger("click"); <!-- HERE --> 
       } 
      }); 
     }); 
     --> 
    </script> 
</head> 
<body> 
    <div id="container"> 
     <from id="databaseActionForm" name="databaseActionForm" method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"> 
      <table border=0 style="margin: 50px auto; text-align: right;"> 
       <tr style="text-align: center; font-weight: bold; font-size: 1.5em; text-decoration: underline;"> 
        <th>Action</th> 
        <th>Additional Info</th> 
        <th>Button</th> 
       </tr> 
       <tr name="Clear"> 
        <td>Erase Database</td> 
        <td style="text-align: center;">ARE YOU SURE?</td> 
        <td><input type="button" id="erase" name="erase" value="ERASE ALL?" /></td> 
       </tr> 
       <tr name="Update"> 
        <td>Update Value</td> 
        <td> 
         Entry ID: <input type="text" id="updateAppID" name="updateAppID" placeholder="App entryID" /><br /> 
         Field Name: <input type="text" id="fieldName" name="fieldName" placeholder="Field to change" /><br /> 
         New Value: <input type="text" id="newValue" name="newValue" placeholder="New value" /><br /> 
        </td> 
        <td><input type="button" id="update" name="update" value="Update Value" /></td> 
       </tr> 
       <tr name="Delete"> 
        <td>Delete Payment</td> 
        <td> 
         Entry ID: <input type="text" id="deleteAppID" name="deleteAppID" placeholder="App entryID" /> 
        </td> 
        <td><input type="button" id="delete" name="delete" value="Delete Entry" /></td> 
       </tr> 
      </table> 
      <input type="hidden" id="buttonTypePressed" name="buttonTypePressed" /> 
      <input type="submit" id="submit" name="submit" value="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1"/> 
     </form> 
    </div> 
</body> 

+0

嘗試'document.databaseActionForm.submi ();' –

+0

@ Karthikeyan.V嘗試過並且也是'document.getElementByID('databaseActionForm')。submit();'並且它不起作用 –

+0

你試過用'

'而不是' '元素? – nnnnnn

回答

1

有2個問題在這裏,1是該元素的名稱form一個錯字,你有它爲from

另一種是提交按鈕的名稱/ ID,它不應該是submit,因爲它會覆蓋form元素

<input type="submit" id="bsubmit" name="bsubmit" value="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" /> 

然後,只需使用下面的代碼段的默認submit屬性(功能)提交表單

$('#databaseActionForm').submit(); 

演示:Fiddle

+0

YUP,拼寫錯誤是問題。我坐在那裏和JS一起玩了幾個小時,這只是一個拼寫錯誤。恥辱! –

1

我認爲這個問題是你拼錯的 「形式」 爲 「從」。檢查語法突出顯示。之後,所有的這些都可以工作:

$('form#databaseActionForm').submit(); 

$('#databaseActionForm').submit(); // referencing the form's ID 

document.databaseActionForm.submit(); // referencing the form's NAME