大网数据广告活动
Web 标准与前端开发

HTMLDialogElement: returnValue property

· 52CE 我爱测

The **`returnValue`** property of the HTMLDialogElement interface is a string representing the return value for a dialog element when it's closed. You can set the value directly (`dialog.returnValue = "result"`) or by providing the value as a string argument to HTMLDialogElement.

HTML DOM

The returnValue property of the HTMLDialogElement interface is a string representing the return value for a dialog element when it's closed. You can set the value directly (dialog.returnValue = "result") or by providing the value as a string argument to HTMLDialogElement.close() or HTMLDialogElement.requestClose().

Value

A string representing the returnValue of the dialog. Defaults to an empty string ("").

Examples

Checking the return value

The following example displays a button to open a dialog. The dialog asks the user if they want to accept a Terms of Service prompt.

The dialog contains "Accept" or "Decline" buttons: when the user clicks one of the buttons, the button's click handler closes the dialog, passing their choice into the HTMLDialogElement.close() function. This assigns the choice to the dialog's returnValue property.

In the dialog's HTMLDialogElement.close_event event handler, the example updates the main page's status text to record the returnValue.

If the user dismisses the dialog without clicking a button (for example, by pressing the <kbd>Esc</kbd> key), then the return value is not set.

HTML

<dialog id="dialog">
  <p>Do you agree to the Terms of Service (link)?</p>
  <button id="decline" value="declined">Decline</button>
  <button id="accept" value="accepted">Accept</button>
</dialog>
<button id="open">Open dialog</button>
<pre id="log"></pre>
#log {
  height: 170px;
  overflow: scroll;
  padding: 0.5rem;
  border: 1px solid black;
}
const logElement = document.getElementById("log");
function log(text) {
  logElement.innerText = `${logElement.innerText}${text}\n`;
  logElement.scrollTop = logElement.scrollHeight;
}

JavaScript

const dialog = document.getElementById("dialog");
const openButton = document.getElementById("open");
const declineButton = document.getElementById("decline");
const acceptButton = document.getElementById("accept");

openButton.addEventListener("click", () => {
  // Reset the return value on each open
  dialog.returnValue = "";
  updateReturnValue();
  // Show the dialog
  dialog.showModal();
});

function closeDialog(event) {
  const button = event.target;
  dialog.close(button.value);
}

function updateReturnValue() {
  log(`Return value: "${dialog.returnValue}"`);
}

declineButton.addEventListener("click", closeDialog);
acceptButton.addEventListener("click", closeDialog);

dialog.addEventListener("close", updateReturnValue);

Result

Click "Open Dialog", then choose the "Accept" or "Decline" buttons in the dialog, or dismiss the dialog by pressing the <kbd>Esc</kbd> key. Observe the different status updates.

Checking the return value

Specifications

Browser compatibility

See also

  • HTML dialog element

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。