Javascript ajax成功后在文本框中保留值
我有一个选择框,其中包含选项X和O。我在其上放置了一个函数Onchange,以填充文本框中的值Javascript ajax成功后在文本框中保留值,javascript,local-storage,Javascript,Local Storage,我有一个选择框,其中包含选项X和O。我在其上放置了一个函数Onchange,以填充文本框中的值 <input type="text" id="remarks1"/> ajax成功后,我现在如何保留textbox的值?您可以使用存储值,以便以后使用: function myFunction(){ var dropdown1= document.getElementById("Remarks"); ////===== select box var selection1
<input type="text" id="remarks1"/>
ajax成功后,我现在如何保留textbox的值?您可以使用存储值,以便以后使用:
function myFunction(){
var dropdown1= document.getElementById("Remarks"); ////===== select box
var selection1 = dropdown1.value;
localStorage.setItem('remarks', selection1); // set the value in localStorage
var emailTextBox1 = document.getElementById("remarks1"); //==== textbox
emailTextBox1.value = selection1;
}
然后在AJAX成功案例中:
我不知道,但这对我不起作用。当页面重新加载时,Textbox仍然没有值。@Gelxxxx,可能还有其他一些问题,因为它在逻辑上应该可以工作。尝试在控制台中查看localStorage值…是的,Remark1在控制台中的localStorage中有一个值。但它应该显示在文本框中,对吗?但是它没有显示它的值。@Gelxxxx,您可以在ajax成功中调试该值。我猜文本框的值在成功后重置了一些地方……是的,我猜文本框也重置了它的值,但我不知道它在哪里。
$(document).on('click','#btnSave',function(){
var employeeName = document.getElementById('employeeName').value;
var r1 = document.getElementById('remarks1').value;
var r2 = document.getElementById('remarks2').value;
$.ajax({
type: 'post',
url: 'update_data.php',
data: {
'DAY1' :r1,
'DAY1_A' :r2,
'employeeName' :employeeName
},
success: function(data){
$("#content").html(data)
$(".loader").fadeOut("very slow");
$("#content").hide().fadeIn(500)
alert("Changes Succesfully Saved!");
},
error:function(data){
alert('Failed');
}
})
});
function myFunction(){
var dropdown1= document.getElementById("Remarks"); ////===== select box
var selection1 = dropdown1.value;
localStorage.setItem('remarks', selection1); // set the value in localStorage
var emailTextBox1 = document.getElementById("remarks1"); //==== textbox
emailTextBox1.value = selection1;
}
success: function(data){
$("#content").html(data)
$(".loader").fadeOut("very slow");
$("#content").hide().fadeIn(500)
var remarks = localStorage.getItem('remarks'); // get from localStorage
document.getElementById("remarks1").value = remarks; // set the value in the text box;
alert("Changes Succesfully Saved!");
}