我有一个购物车系统,客户可以在该系统中接受订单并检查购物车中是否有东西,但如果购物车中没有东西,则无法继续。

如果他们单击“提交”按钮,则会弹出警报。我有这个的JavaScript。

function validateForm()
{
var x=document.forms["form1"]["total"].value;
if (x==null || x=="")
  {
  alert("Take your order first.");
  return false;
  }
var con = confirm("Are you sure you want to order this item/s?");
if (con ==false)
{
return false;
}
}


单击弹出消息上的“确定”后,如何更改提交按钮的文本?我希望当客户一次添加他/她的订单时将其更改为“更新订单”。

最佳答案

你是这个意思?



document.getElementById("myForm").addEventListener("submit",function(e) {
  var x = this.total.value.trim(), sure = false;
  if (this.subBut.value=="Update order" && x !=="") return true;
  e.preventDefault();
  if (x == "") {
    alert("Please take your order first.");
  }
  else sure =  confirm("Are you sure you want to order this item/s?");
  if (sure) {
    this.subBut.value="Update order";
  }
  return sure;
});

<form id="myForm">
<input type="text" name="total" value="" />
<input type="submit" name="subBut" value="Add order" />
</form>

关于javascript - 单击警报弹出窗口上的“确定”时更改提交按钮的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54882306/

10-12 12:46
查看更多