Mailto on submit button

后端 未结 6 1424
旧巷少年郎
旧巷少年郎 2020-11-28 14:46

Is it possible to implement mailto: function on submit button like ? I use ASP.NET MVC. Maybe there is some tricky c

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 15:28

    What you need to do is use the onchange event listener in the form and change the href attribute of the send button according to the context of the mail:


    Create Message

    JavaScript

    function mail(form) {
        var name = form.name.value;
        var city = "";
        var adate = form.adate.value;
        var ddate = form.ddate.value;
        var activities = form.activities.value;
        var adult = form.adult.value;
        var child = form.childeren.value;
        var comment = form.comment.value;
        var warning = ""
        for (i = 0; i < form.city.length; i++) {
            if (form.city[i].checked)
                city += " " + form.city[i].value;
        }
        var str = "mailto:abc@x.com?subject=travel to morocco&body=";
        if (name.length > 0) {
            str += "Hi my name is " + name + ", ";
        } else {
            warning += "Name is required"
        }
        if (city.length > 0) {
            str += "I am Intersted in visiting the following citis: " + city + ", ";
        }
        if (activities.length > 0) {
            str += "I am Intersted in following activities: " + activities + ". "
        }
        if (adate.length > 0) {
            str += "I will be ariving on " + adate;
        }
        if (ddate.length > 0) {
            str += " And departing on " + ddate;
        }
        if (adult.length > 0) {
            if (adult == 1 && child == null) {
                str += ". I will be travelling alone"
            } else if (adult > 1) {
                str += ".We will have a group of " + adult + " adults ";
            }
            if (child == null) {
                str += ".";
            } else if (child > 1) {
                str += "along with " + child + " children.";
            } else if (child == 1) {
                str += "along with a child.";
            }
        }
    
        if (comment.length > 0) {
            str += "%0D%0A" + comment + "."
        }
    
        if (warning.length > 0) {
            alert(warning)
        } else {
            str += "%0D%0ARegards,%0D%0A" + name;
            document.getElementById('send').href = str;
        }
    }
    

提交回复
热议问题