2012-06-01 112 views
0

我試圖創建一個提交表單,從而我改變其中一個輸入元素以添加一個cookie的內容,並將此內容與其他輸入元素一起通過電子郵件發送。我嘗試了各種方法,包括訪問value屬性並附加一個子元素,但無濟於事,使用firefox。動態更改提交表單上的輸入元素javascript

HTML

<form method="GET" action="mailto:[email protected]"> 
    <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:[email protected]" 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>"); 
} 

回答

0

使用JavaScript,做如下因素:

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

在頁面加載時,即,

HTML:

<body onload="setNameValue()"> 

的JavaScript

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

希望幫助!

+0

的document.getElementById( 「名稱」)。值。這返回爲空?!根據螢火蟲。 – David

1

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

HTML

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

JS

function getTotal() { 
    var mail = 'mailto:[email protected]'; 
    mail = mail + '?subject=Subject'; 
    mail = mail + '&body=Body'; 
    location.href = mail; 
}; 
相關問題