我需要在<input>中显示<span>的值,并且当标签为空时,将span的值更改为“Video title”。但是我总是在<input>中输入一些内容然后将其删除,但<input>仍具有“”值,因此<span>不显示任何内容。感谢帮助。 (对不起,我的英语不好)

这是我的代码:

$("#newVideoName").keyup(function(){
   var newVideoName = $("#newVideoName").val();
   if(newVideoName == ""){
       $("#newVideoNameLabel").html("Video title");
   }
});
<input type="text" id="newVideoName">
<span id="newVideoNameLabel"></span>

最佳答案

$("#newVideoName").keyup(function() {
   $("#newVideoNameLabel").text(this.value ? this.value : "Video title");
});

http://jsfiddle.net/gZPyQ/

07-24 15:47