2016-05-11 115 views
0

在我的网站,我想这两个代码:如果输入字段为空,禁用提交按钮V2

$(document).ready(function(){ 
    $('.sendButton').attr('disabled', true); 
    $('#message').keyup(function() { 
     if ($(this).val().length != 0) 
      $('.sendButton').attr('disabled', false);    
     else 
      $('.sendButton').attr('disabled',true); 
    }) 
}); 

然后我这个尝试之一:

$(document).ready(function(){ 
    $('.sendButton').prop('disabled', true); 
    $('#message').keyup(function(){ 
     $('.sendButton').prop('disabled', this.value == "" ? true : false);  
    }) 
}); 

如何修改或者那些使我的按钮没有被禁用,当你做的唯一事情是右键点击粘贴在textarea(#message)?使用这些代码只有在您最初单击它时才起作用。如果在textarea中没有文本,我希望该按钮被禁用,但是同时我希望它被启用,如果它有文本但没有初始左键单击。 IE浏览器。你在页面加载时做的唯一事情是右键单击,粘贴,然后单击按钮(它应该可以工作)。

在此先感谢!

PS:两个代码来自这个话题:If input field is empty, disable submit button

+0

尽量结合到'的onChange事件而不是密码。 –

+0

下面是代码的其余部分? 'textarea(#message)' –

回答

1

您可以使用.change()事件处理程序。当输入值改变时它将被调度。所以当有人在场上粘贴时,它会被改变,所以你会得到你的动作。

$(".target").change(function() { 
    alert("Handler for .change() called."); 
}); 
+0

恐怕我无法解释你的答案,因为我是一个带有代码的新手。你介意帮我吗?这是我在这个论坛中编写的代码,但是为我的页面进行了修改: Austin

+0

在这里,我已经提供了codepen,但改变了一下(不使用.change())http://codepen.io/anon/pen/xVBMWP请注意它不适用于IE <9 。 (''input',function(e){$''readme')。text($('。writeme')。val() ) });' *这里是html * '

Here you'll see changes
' – Mort

0

尝试......

$(document).ready(function(){ 

$(document).ready(function(){ 
    $('.sendButton').prop('disabled', true); 
    $('#message').on('input',function(){  

     $('.sendButton').prop('disabled', this.value === "");  
     }) 
}); 

}); 

延伸到上面的代码,如果任何浏览器有粘贴问题使用输入的propertyChange贴输入

相关问题