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

时间:2012-06-01 19:41:27

标签: javascript

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

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

2 个答案:

答案 0 :(得分:1)

您不需要表格。看看我的小提琴here

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

答案 1 :(得分:0)

使用JavaScript,请执行以下操作:

document.getElementById("Name").value = cookieValue;
页面加载时

,即

<强> HTML:

<body onload="setNameValue()">

<强>的JavaScript

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

希望有所帮助!