小程序顯示模態對話框
wx.showModal(Object object)
顯示模態對話框
參數
Object object
屬性 類型 默認值 必填 說明 最低版本
title string 否 提示的標題
content string 否 提示的內容
showCancel boolean true 否 是否顯示取消按鈕
cancelText string 取消 否 取消按鈕的文字,最多 4 個字符
cancelColor string #000000 否 取消按鈕的文字顏色,必須是 16 進制格式的顏色字符串
confirmText string 確定 否 確認按鈕的文字,最多 4 個字符
confirmColor string #576B95 否 確認按鈕的文字顏色,必須是 16 進制格式的顏色字符串
editable boolean false 否 是否顯示輸入框 2.17.1
placeholderText string 否 顯示輸入框時的提示文本 2.17.1
success function 否 接口調用成功的回調函數
fail function 否 接口調用失敗的回調函數
complete function 否 接口調用結束的回調函數(調用成功、失敗都會執行)
object.success 回調函數
參數
Object res
屬性 類型 說明 最低版本
content string editable 為 true 時,用戶輸入的文本
confirm boolean 為 true 時,表示用戶點擊了確定按鈕
cancel boolean 為 true 時,表示用戶點擊了取消(用于 Android 系統區分點擊蒙層關閉還是點擊取消按鈕關閉) 1.1.0
示例代碼
wx.showModal({
title: '提示',
content: '這是一個模態彈窗',
success (res) {
if (res.confirm) {
console.log('用戶點擊確定')
} else if (res.cancel) {
console.log('用戶點擊取消')
}
}
})
注意
Android 6.7.2 以下版本,點擊取消或蒙層時,回調 fail, errMsg 為 "fail cancel";
Android 6.7.2 及以上版本 和 iOS 點擊蒙層不會關閉模態彈窗,所以盡量避免使用「取消」分支中實現業務邏輯
自基礎庫 2.17.1 版本起,支持傳入 editable 參數,顯示帶輸入框的彈窗