只是尝试使用 jQuery 更改输入的值。似乎有效,但是,当我使用 Firebug 查看 #input 的值时,它仍保持原始值。这是正常现象,还是我做错了什么?谢谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>Testing</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$("#DoIt").click(function()
{
$('#input').val('final');
console.log($('#input'));
alert($('#input').val());
});
});
</script>
</head>
<body>
<a href="#" id="DoIt">DoIt</a>
<input id="input" type="text" value="original" />
</body>
</html>
最佳答案
使用.val()
:console.log( $('#input').val() );
$(function(){
$("#DoIt").click(function(){
$('#input').val('final');
console.log($('#input').val()); // the fixed line!
alert($('#input').val());
});
});
<小时/>
编辑
现在我明白你在看什么了:Firebug 中的元素源(检查元素)内部。
好吧,如果您想看到更改实际发生,您必须使用:
$('#input').attr('value','final');
而不是:
$('#input').val('final');
否则,更改将由浏览器存储(无需担心),但 Firebug 不会更新。
in this demo you can see it working:
demo
$("#DoIt").click(function(e){
$('#input').attr('value','final'); // use .attr() to see the change happen in Firebug!
// $('#input').val('final'); // but rather use this
// $('#input').prop('value','final'); // or better THIS one!
console.log( $('#input').val() );
alert( $('#input').val() );
return false;
});
关于jquery - firebug 不会反射(reflect)更改输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10701977/