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

xcitsw88  于 2021-09-29  发布在  Java
关注(0)|答案(4)|浏览(363)
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>

当有人单击“确定”或“取消”时,我希望页面刷新

p1iqtdky

p1iqtdky1#

2件事,首先将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>
zy1mlcev

zy1mlcev2#

我想你不需要用“如果”这个词

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

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

zysjyyx4

zysjyyx43#

改变 location.reload();window.location.reload(); 看看这能不能满足你的需要。

oymdgrw7

oymdgrw74#

您的功能运行良好,但只需存储 confirm() 在变量中,然后在用户单击时进行检查 ok 回来了 true 当点击 cancel 回来了 false 所以如果你想确认你的工作 cancelok 您需要检查返回值,如下所示

function clicked() {
    var conf = confirm('You just clicked the button, click ok or cancel to refresh.')
    console.log(conf);
     if (conf === true || conf === false) {
           yourformelement.submit();
           location.reload();
       } else {
           return false;
       }
    }

相关问题