Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 保留html输入的旧值_Jquery_Html_Input - Fatal编程技术网

Jquery 保留html输入的旧值

Jquery 保留html输入的旧值,jquery,html,input,Jquery,Html,Input,我需要获取输入框的旧值 <input type ="text" name="" onFocus=(this.name=this.value) onchange='doSomething(this.value,this.name)'/> <input type ="text" name="" onFocus=(this.name=this.value) autofocus=(this.name=this.value) onchange='doSomething(this.val

我需要获取输入框的旧值

<input type ="text" name="" onFocus=(this.name=this.value) onchange='doSomething(this.value,this.name)'/>
<input type ="text" name="" onFocus=(this.name=this.value) autofocus=(this.name=this.value) onchange='doSomething(this.value,this.name)'/>
如果我读取输入的name
attr
,我将获得该值

$(input).attr("name");

若否,;是否还有其他帮助。

根据@RoryMcCrossan的建议,您可以尝试使用jQuery.data()方法将值存储到焦点上的数据属性中,例如

HTML:

然后,您可以使用以下方法获取值:

jQuery(input).data("myoldvalue");

看看
这个.defaultValue
你的输入上没有名字吗?那么名称是什么?@DarrenSweeney typo搞错了为什么要更改表单元素的
名称
,以匹配其值?当然,您的服务器端代码取决于
名称
是否始终相同?@rorymcrossan以保留旧值。需求让你的手很脏
jQuery("input").on("focus", function(e) {
   var elem = jQuery(e.target);
   console.log('oldval', elem.data('myoldvalue')); //Some debugging code
   elem.data('myoldvalue', elem.val());
   console.log('new-oldval', elem.data('myoldvalue')); //Some debugging code
});
jQuery(input).data("myoldvalue");