简体   繁体   中英

Button onclick won't work for Javascript query

Hi I wonder if anyone can see what I'm doing wrong,

I've got a button which opens an email with the results of a form,

I can make it work fine if I do it as a text link

<a href="javascript:doMailTo(' ');">Click here to email </a>

However I can't make the button work at all

<button onclick="doMailTo(' ');" value="toEmail">Send to Email</button>

I've been playing around with variations but I can't make it work!!

The link and the button can be right next to each other, and the text link works when the button doesn't!

Can anyone see what I'm doing wrong?

The Function itself is

<script>
function doMailTo(addr) {
    location.href = "mailto:" + addr + "?subject=Flight Itinerary&body=" +
          encodeURI(document.getElementById('p3').textContent);
}
 </script>

And it's located in the header

Try for this on local system:

May This will work for you

 $(document).ready(function() { $('#bt1').click(function() { $('#fr1').attr('action', 'mailto:test@test.com?subject=' + $('#tb1').val() + '&body=' + $('#tb2').val()); $('#fr1').submit(); }); }); 
 <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <form id="fr1"> <input type="text" id="tb1" /> <input type="text" id="tb2" /> <input type="button" id="bt1" value="click" /> </form> </body> </html> 

Buttons in a form default to type="submit" so when clicked, it tries to submit the form. Specify type="button" .

<button type="button" onclick="doMailTo(' ');" value="toEmail">Send to Email</button>

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