0

如果文本输入为空或不在 Jquery 语句中,我将如何比较它?下面的代码可以吗?

$(#'generate').click(function(){
   if(parseInt($('#lastname').val,10)==0){
      alert("Should Not Be Empty!");
   }
}
4

3 回答 3

1

$(#'generate')should be$('#generate')$('#lastname').valshould $('#lastname').val(),因为val()是一种方法。

$('#generate').click(function(){
   if( !$.trim( $('#lastname').val() ) ){
      alert('Should Not Be Empty!');
   }
}

但是您尝试parseInt($('#lastname').val(),10)了 which 将输出NaNif, input box 为空,而不是 give 0

于 2012-09-30T06:15:17.757 回答
1

你的意思是:

if($.trim( $('#lastname').val() ) != ""){
    alert("Should Not Be Empty!");
}

或者

if($.trim( $('#lastname').val() ).length  > 0){
    alert("Should Not Be Empty!");
}
于 2012-09-30T06:02:36.317 回答
0

简单地

$('#generate').click(function(){
    if($('#lastname').val().trim() != "") {
       alert("Should Not Be Empty!");
    }
}

另外,有错别字

  $(#'generate').click(function(){
//   ^   Right here
于 2012-09-30T06:14:00.253 回答