在提交表单上动态更改输入元素 javascript

Dynamically alter input element javascript on submit form

本文关键字:输入 元素 javascript 动态 提交 表单      更新时间:2023-09-26

我正在尝试创建一个提交表单,通过该表单,我更改其中一个输入元素以添加cookie的内容,并将该内容与其他输入元素一起通过电子邮件发送。我已经尝试了各种方法,包括访问 value 属性和附加子元素,但使用 Firefox 无济

于事。

.HTML

<form method="GET" action="mailto:davidflynn12@hotmail.com">
    <input type="hidden" name="subject" value="javascript:GetTotal()">
    <input type="hidden" name="body" value="test">
    <input type="submit" value="Send Email">
</form>
<form id="form1" action="mailto:admin@example.com" enctype="text/plain"
method="post">
    <a href="javascript:addOption()">Add Details </a>
    <p>Name:
        <input name="Name" type="text" id="Name" size="40" value=javascript:GetTotal()>
    </p>
    <p>E-mail address:
        <input name="E-mail" type="text" id="E-mail" size="40">
    </p>
    <p>Comment:</p>
    <p>
        <textarea name="Comment" cols="55" rows="5" id="Comment"></textarea>
    </p>
    <p>
        <input type="submit" name="Submit" value="Submit">
    </p>
</form>

.JS

function GetTotal() {
    // Get the individual cookies, by splitting the cookie string
    // Individual cookies is now an array of all the name=value pairs
    var individualCookies = document.cookie.split(';');
    var temp3 = 0;
    //document.write("<p class = 'special_p'>");
    for (var i = 0; i < individualCookies.length; i++) {
        // Loop through each of the cookies, and split them into
        // their name and value, as separate elements in the array
        var oneCookie = individualCookies[i].split("=");
        var name = oneCookie[0];
        var value = oneCookie[1];

        // This is standard code for removing excess white space
        name = name.replace(/^'s+|'s+$/g, '');
        //document.write("<ul>");
        document.write("<ul>");
        if (name == 'History of Boxing') {
            var temp = 7;
            var temp2 = temp * value;
            document.write("<li class = 'special_p'>" + name + " By David
                                Flynn. No. of Copies: " + value + ".Total cost
                                =" + temp2 + "Euros</li>");
        } else if (name == 'Future of Boxing') {
            var temp = 8;
            var temp2 = temp * value;
            document.write("<li class = 'special_p'>" + name + " By
                                    David Flynn. No. of Copies: " + value + ".Total cost =
                                    " + temp2 + "Euros</li>");
        } else if (name == 'History of TaeKwondo') {
            var temp = 9;
            var temp2 = temp * value;
            document.write("<li class =
                                                'special_p'>" + name + " By David Flynn. No.
                                                of Copies: " + value + ".Total cost =
                                                " + temp2 + "Euros</li>");
        } else if (name == 'Future of TaeKwondo') {
            var temp = 10;
            var temp2 = temp * value;
            document.write("<li class =
                                                'special_p'>" + name + " By David Flynn. No.
                                                of Copies: " + value + ".Total cost =
                                                " + temp2 + "Euros</li>");
        }
        document.write("</ul>");
        temp3 += temp2;
        //document.write("Grand total = "+temp3"):
    }
    document.write("<p class = 'special_p'>Total bill is
                                " + temp3 + " Euros</p>");
    //document.write("</p>");
}

你不需要表格。在这里查看我的小提琴。

.HTML

<input type="button" value="Send Email" onclick="getTotal()"/>

.JS

function getTotal() {
    var mail = 'mailto:davidflynn12@hotmail.com';
    mail = mail + '?subject=Subject';
    mail = mail + '&body=Body';
    location.href = mail;
};

使用 JavaScript,执行以下操作:

document.getElementById("Name").value = cookieValue;

当页面加载时,即

.HTML:

<body onload="setNameValue()">

JavaScript

function setNameValue(){
    document.getElementById("Name").value = cookieValue;
}

希望对您有所帮助!