简体   繁体   中英

Change the text of submit button when clicked OKAY on alert pop-up

I have a shopping cart system where a customer can take orders and check out if they have anything on the cart but can't continue if it has nothing.

An alert will pop-up if they click on Submit button. I have this javascript for that one.

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;
}
}

How can I change the text of the submit button when the Okay is clicked on the pop-up message? Instead of "Add Order" I want it to be changed to "Update order" when the customer added his/her order once.

You mean this?

 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> 

Of corse it is not best solution because we can`t see all the code, but if I understand you right

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;
    } else {
        var submittBtn = document.forms["form1"][your_button_name];
        submittBtn.txt = "Update order"; 
    }
}

Please note when you reset form or customer delete his order you need to change the button text back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM