Jquery:如果值与加载页面时的值相同,则在按下提交按钮时删除值
当按下submit按钮时,如何删除每个输入的字段值,该字段值与使用Jquery加载页面时的值相同 逻辑示例: 将变量设置为加载时的每个输入值 鼠标单击submit按钮向下移动时,如果该值仍与该变量匹配,则删除该值Jquery:如果值与加载页面时的值相同,则在按下提交按钮时删除值,jquery,forms,input,Jquery,Forms,Input,当按下submit按钮时,如何删除每个输入的字段值,该字段值与使用Jquery加载页面时的值相同 逻辑示例: 将变量设置为加载时的每个输入值 鼠标单击submit按钮向下移动时,如果该值仍与该变量匹配,则删除该值 对于jQuery还是新手,感谢您的帮助:)我将使用的一种方法是设置自定义HTML属性,例如: <input name="foo" value="test" data-default="test" /> 希望这对您有所帮助。在所有输入字段中添加一个onChange事件,以跟
对于jQuery还是新手,感谢您的帮助:)我将使用的一种方法是设置自定义HTML属性,例如:
<input name="foo" value="test" data-default="test" />
希望这对您有所帮助。在所有输入字段中添加一个onChange事件,以跟踪修改的字段 在另一个属性中存储默认值将增加页面加载时间并复制所有数据。也许你的数据很丰富。如果是这样的话,请注意变化 示例:
var untouched_fields = []
$(document).ready(function() {
$('#form input').each(function() { untouched_fields.push( $(this).attr("id") ) }
$('#form input').on("change", function() { untouched_fields.remove( $(this).attr("id") ) })
})
…然后在提交时,读取未触及的_字段。首先,如果浏览器支持占位符,请使用占位符而不是您的功能。接下来,如果不支持占位符,则首先相应地更新输入,然后在提交时撤消该更改 HTML:
<input name="first-name" placeholder="First Name" />
// before all of your code
jQuery.support.placeholder = (function(){
var i = document.createElement('input');
return 'placeholder' in i;
})();
// in document ready
if (!$.support.placeholder) {
// update value of empty inputs to the value of the placeholder attribute if set.
$("input[placeholder]").val(function(i,val) {
return val == "" ? $(this).attr("placeholder") || "" : val;
}).focus(function(){
if (this.value == $(this).attr("placeholder")) {
this.value = "";
}
}).blur(function(){
if (this.value == "") {
this.value = $(this).attr("placeholder");
}
});
}
$('form').submit(function() {
if (!$.support.placeholder) {
$("input[placeholder]",this).val(function(i,val) {
return val == $(this).attr("placeholder") ? "" : val;
});
}
});
在我自己的代码中,我实际上会在输入后面放置一个标签,并显示/隐藏它,而不是触摸输入的值。出于好奇,这是为了什么?您是否正在实现占位符,然后试图在提交时删除它?是的!有更好的方法吗?是的,使输入的背景色透明,然后在其后面浮动一个带有占位符文本的标签。然后,当用户开始输入时,根据是否有值,使用keydown事件和短setTimeout隐藏标签或显示标签。或者删除数据属性并使用
this.defaultValue
替换为code我没有使用它,尽管它为我提供了正确的实现方法,谢谢!
// before all of your code
jQuery.support.placeholder = (function(){
var i = document.createElement('input');
return 'placeholder' in i;
})();
// in document ready
if (!$.support.placeholder) {
// update value of empty inputs to the value of the placeholder attribute if set.
$("input[placeholder]").val(function(i,val) {
return val == "" ? $(this).attr("placeholder") || "" : val;
}).focus(function(){
if (this.value == $(this).attr("placeholder")) {
this.value = "";
}
}).blur(function(){
if (this.value == "") {
this.value = $(this).attr("placeholder");
}
});
}
$('form').submit(function() {
if (!$.support.placeholder) {
$("input[placeholder]",this).val(function(i,val) {
return val == $(this).attr("placeholder") ? "" : val;
});
}
});