$(#email).attr('value')
應該給出原始的輸入默認值,但是使用jQuery 1.8.1,給出當前值。爲什麼是這樣?這個改變什麼時候發生?我應該採取哪些措施,以便在發現事情不像預期的那樣工作後,我可以意識到這些變化,而不會偶然發現這些變化。謝謝
http://jsfiddle.net/ufrHD/2/使用jQuery 1.8.1
http://jsfiddle.net/ufrHD/3/使用jQuery 1.9.1
<!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>Input</title>
<script src="http://code.jquery.com/jquery-1.8.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#click").click(function() {alert("$('#email').val()="+$('#email').val()+"\n$('#email').attr('value')="+$('#email').attr('value'));});
});
</script>
</head>
<body>
<button id="click">Click</button>
<input type="email" name="name" id="email" value="[email protected]" />
</body>
</html>
我不明白你爲什麼要使用'.attr('value')',或者你爲什麼期望它給你'defaultValue'? – billyonecan 2013-05-07 13:41:21
正如John Resig所說,「.attr()和.prop()都不應該用於獲取/設置值。使用.val()方法取而代之(儘管使用.attr(」value「,」somevalue「)將會繼續像1.6之前那樣工作)「。 – j08691 2013-05-07 13:41:47
@billyonecan。我正在嘗試獲取原始默認值。這還能做什麼? – user1032531 2013-05-07 13:42:16