防止在"返回"时提交表单,但在 Edge 上按,但不在 Chrome/Firefox 上



我在html中有一个"details"标签,当用户按回车键时打开和关闭。在边缘,当用户在"详细信息"标签上按 Enter 时,表单将提交。我正在处理的任务要求在标签上按 Enter 时阻止提交。

我目前对解决方案的尝试是:

var returnKey = 13;
var openProperty = 'open';
$('details').keypress(function(event){
    if ( event.which == returnKey ) {
      event.preventDefault();
      if ( $(this).hasOwnProperty(openProperty) ) {
        $(this).removeProp("open");
      } else {
        $(this).prop("open", true);
      }
    }
}); 

这修复了IE/Edge中由于preventDefault而导致的问题,但是在chrome和Firefox中,"details"标签不再打开和关闭。添加此部分是为了尝试阻止这种情况:

   if ( $(this).hasOwnProperty(openProperty) ) {
    $(this).removeProp("open");
  } else {
    $(this).prop("open", true);
  }

这将打开标签,但永远不会允许它关闭。

尝试使用 .attr() 方法在属性的值之间切换:

if ( $(this).attr(openProperty) ) {
  $(this).removeAttr("open");
} else {
  $(this).attr("open", '');
}

最新更新