2013-04-08 16 views
0

進出口試圖修改此腳本召回形式值不工作的文字區域

http://www.dynamicdrive.com/dynamicindex16/formremember2.htm

爲文字區域工作,而不是隻輸入文本框。下面有什麼即時猜測腳本,我只是不能想出辦法來的相關部分是我自己

rememberForm.prototype.savevalues=function(){ //get form values and store in cookie 
for (var i=0; i<this.fields.length; i++){ 
if (this.fields[i].type=="text") 
this.cookiestr+=this.fields[i].fname+":"+escape(this.fields[i].value)+"#" 
} 
if (typeof this.togglebox!="undefined"){ //if "remember values checkbox" is defined 
this.persistdays=(this.togglebox.checked)? this.persistdays : -1 //decide whether to  save form values 
this.cookiestr=(this.togglebox.checked)? this.cookiestr+"toggleboxid:on;" : this.cookiestr 
} 
else //if checkbox isn't defined, just remove final "#" from cookie string 
this.cookiestr=this.cookiestr.substr(0, this.cookiestr.length-1)+";" 
setCookie(this.cookiename, this.cookiestr, this.persistdays) 
} 

rememberForm.prototype.recallvalues=function(){ //populate form with saved values 
var cookievalue=getCookie(this.cookiename) 
if (cookievalue!=""){ //parse cookie, where cookie looks like: field1:value1#field2:value2... 
var cookievaluepair=cookievalue.split("#") 
for (var i=0; i<cookievaluepair.length; i++){ 
if (cookievaluepair[i].split(":")[0]!="toggleboxid" && this.getfield(cookievaluepair[i].split(":")[0]).type=="text") 
this.getfield(cookievaluepair[i].split(":")   [0]).value=unescape(cookievaluepair[i].split(":")[1]) 
else //else if name in name/value pair is "toggleboxid" 
this.togglebox.checked=true 
} 
} 
+0

你意識到這是不可能讀取! – 2013-04-08 21:53:46

回答

0

persistfields(id, ...)設置你想在cookie中堅持的字段的方法。這些字段是由id查找的,所以我想添加一個textareaid屬性就足夠了。

例如:

<form id="myFormId"> 
    <input type="text" id="someInputId" /> 
    <textarea id="textareaId"></textarea> 
</form> 

<script> 
    var f = new rememberForm('myFormId'); 
    f.persistfields('someInputId', 'textareaId'); 
</script> 

這將輸入和文本區域添加到rememberForm實例fields財產。


UPDATE

的問題出在rememberForm此方法。爲了便於閱讀,我對代碼進行了格式化,因爲原始資源有可怕的格式。

rememberForm.prototype.savevalues = function() { 

    for (var i = 0; i < this.fields.length; i++) { 

     // PROBLEM: only allows type="text" 
     if (this.fields[i].type == "text") { 
      this.cookiestr += this.fields[i].fname + " : " + escape(this.fields[i].value) + "#" 
     } 
     if (typeof this.togglebox != "undefined") { 
      this.persistdays = (this.togglebox.checked) ? this.persistdays : -1; 
      this.cookiestr = (this.togglebox.checked) ? this.cookiestr + "toggleboxid:on;" : this.cookiestr 
     } else { 
      this.cookiestr = this.cookiestr.substr(0, this.cookiestr.length - 1) + ";" 
      setCookie(this.cookiename, this.cookiestr, this.persistdays) 
     } 
    } 
} 

正如我在評論中提到的那樣,它將測試input元素的類型爲'text'。如果你想在Cookie中添加文字區域,你可以把上面一行:

if (this.fields[i].type == "text" || this.fields[i].type == 'textarea') { 

這應該工作。

+0

是的,我認爲它也會,但它不工作 – 2013-04-08 23:50:05

+0

你可以提供一個[小提琴](http://jsfiddle.net)你到目前爲止的代碼? – Bart 2013-04-09 10:15:01