简体   繁体   中英

Having 2 actions occur on button click javascript

I do not know much javascript and I am trying to pull in a form's input values, construct the link to pass the values to and send the user to that link on button click.

I am not sure how to make both of those actions happen onClick.

Here is what I currently have:

<script>
function myFunction() {
  var firstname = document.getElementById("firstname").value;
  var lastname = document.getElementById("lastname").value;
  var email = document.getElementById("email").value;
  var phone = document.getElementById("phone").value;
  var link = 'https://app.squarespacescheduling.com/schedule.php\?owner=21917237&appointmentType=20129186&firstName=' + firstname + '&lastName=' + lastname + '&email=' + email + '&phone=' + phone;
}
</script>

And this is what I am trying inline with my button element:

<div class="de elBTN elAlign_center elMargin0 ui-droppable de-editable" id="tmp_button-86409" data-de-type="button" data-de-editing="false" data-title="button" data-ce="false" data-trigger="none" data-animate="fade" data-delay="500" style="margin-top: 30px; outline: none; cursor: pointer; display: block;" aria-disabled="false" data-elbuttontype="1">
<a onclick={myFunction(), onclick="location.href=link;"} class="elButton elButtonSize1 elButtonColor1 elButtonRounded elButtonPadding2 elBtnVP_10 elButtonCorner3 elBtnHP_25 elBTN_b_1 elButtonShadowN1 elButtonTxtColor1 elButtonFull" style="color: rgb(255, 255, 255); font-weight: 600; background-color: rgb(1, 116, 199); font-size: 26px;">
<span class="elButtonMain"><i class="fa_prepended fa fa-angle-double-right"></i>Let's Get Started<i class="fa_appended fa fa-angle-double-left"></i></span>
<span class="elButtonSub"></span>
</a>
</div>

I need to send the user to the link created in var link. What's the best way to have this happen?

Thanks!

Group the actions in one function can satisfy your need.

//function declaration
<script>
function myFunction() {
  var baseUrl = 'https://app.squarespacescheduling.com/schedule.php\owner=21917237&appointmentType=20129186'
  
  var data = {
    firstname: document.getElementById("firstname").value,   //if using ts, can add ? for checking the existence of ```document.getElementById('firstname')?.value
    lastname: document.getElementById("lastname").value,
    email: document.getElementById("email").value,
    phone: document.getElementById("phone").value,
  }
  
  //do some checking
  var unvalidElem = Object.values(data).filter(eachValue => eachValue !== undefined || eachvalue !== null)

  //if all elements valid, then go to the link
  if(unvalidElem.length === 0){
      location.href = `${baseUrl}&firstName=${firstname}&lastName=${lastname}&email=${email}&phone=${phone}`
  }
</script>


//actually invoke
<a onclick={myFunction();"}

Add this at the bottom of your function, once you've constructed your url:

   location.href = "yourUrl";

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