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)'/>
如果我读取输入的nameattr
,我将获得该值
$(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");