单击按钮后刷新页面(确定/取消)

时间:2021-07-27 00:58:33

标签: javascript html css

function clicked() {
  if (confirm('You just clicked the button, click ok or cancel to refresh.')) {
    yourformelement.submit();
    location.reload();
  } else {
    return false;
  }
}
<button class="center" type="submit" style="border: 0; background: transparent">
    <img src="Playbutton.png" width="800" height="400" alt="submit" onclick="clicked();" />
</button>

我希望在有人点击“确定”或“取消”时刷新页面

4 个答案:

答案 0 :(得分:0)

两件事,首先将 onclick 处理程序放在按钮本身而不是图像上,其次您的 else 语句中没有重新加载调用。

const yourformelement = {};
yourformelement.submit = () => {};


function clicked() {
  if (confirm('You just clicked the button, click ok or cancel to refresh.')) {
    yourformelement.submit();
    location.reload();
  } else {
    location.reload();
    return false;
  }
}
<button class="center" type="submit" style="border: 0; background: transparent" onclick="clicked()">
    <img src="https://kriya-materials.com/wp-content/uploads/2018/01/Play-Button-Transparent-PNG.png" width="400" height="400" alt="submit"/>
</button>

答案 1 :(得分:0)

我认为你不需要使用“if”语句

function clicked() {
    confirm('You just clicked the button, click ok or cancel to refresh.');
    yourformelement.submit();
    location.reload();
}

我建议使用 alert() 来仅显示消息。

答案 2 :(得分:-1)

改变

location.reload();

window.location.reload();

看看这是否能满足您的需求。

答案 3 :(得分:-1)

  • 您的函数运行良好,但您只需要将 confirm() 存储在变量中,然后检查用户点击 ok 时返回 true,点击 cancel 时返回 { {1}} 因此,如果您希望确认在 falsecancel 中工作,则需要检查返回值,如下所示
ok
相关问题