Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 防止提交事件時觸發多個表單按鈕的方法

編輯:關於 Android 程式設計

單擊拍照 button 會執行 getPhoto() 函式,但窗體的 funcSubmit()(ng-submit)也會自動執行。要如何更改才能避免這種情況?也就是只執行 getPhoto(),而不觸發表單的 ng-submit。
補充:這段程式碼是應用程式的一部分,使用 Ionic 框架開發的 Android 行動版。

 <ion-view title="OS"> <form ng-submit = "funcSubmit()"> <ion-content class="has-header"> <ion-list > <ion-item > <button name="fota" class="button button-block button-positive" ng-click="getPhoto()"> <i class="icon ion-ios7-camera"> Photo</i> </button> </ion-item > </ion-list > </ion-content> <div class="bar bar-footer bar-stable"> <button name="canc" class="button button-light" ui-sref="app.padronis">Cancel</button> <button name="subm" class="button button-light" type="submit">Save</button> </div> </form> </ion-view> 

解決方法:把該按鈕改用 type="button" 的 input:

 <input type="button" name="fota" ng-click="getPhoto()">

因為 <button> 在未指定 type 時預設等同於 submit,會觸發表單提交;改用 type="button" 後就只會執行 ng-click 指定的 getPhoto(),不會再觸發 ng-submit。

以上所述就是本文的全部內容了,希望大家能夠喜歡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved