2016-09-27 45 views
0

我试图(单击GET)取值不等于空条件满足,然后将行追加到最终表,但首先其空值为true。如何解决它?如果条件未验证我的值

$(document).ready(function(){ 
 
var a=''; 
 
var b=''; 
 
var c=''; 
 
var d=''; 
 
$('#getrow').click(function(){ 
 
a = $('#empname').val(); 
 
b = $('#age option:selected').val(); 
 
c = $('#gender option:selected').val(); 
 
d = $('#salary option:selected').val(); 
 

 
if(a!=null && b!=null && c!=null && d!=null){ 
 
    alert('Passed'); 
 
    //alert(a+" "+b+" "+c+" "+d); 
 
    $('#final').append('<tr height=\"25\"><td>'+a+'</td><td>'+b+'</td><td>'+c+'</td><td>'+d+'</td></tr>'); 
 

 
    $('#empname').val(' '); 
 
    $('#age').val(' '); 
 
    $('#gender').val(' '); 
 
    $('#salary').val(' '); 
 
} 
 
    else{ 
 
    alert('Failed'); 
 
    } 
 
    
 

 
}); 
 
});
table,tr,th,td{ 
 
     border:1px solid #dddddd; 
 
     border-collapse:collapse; 
 
    } 
 
\t .td-bg{ 
 
\t background:#006597; 
 
\t color:#fff; 
 
\t opacity:0.7; 
 
\t cursor:pointer; 
 
\t } 
 
\t .block-header{ 
 
\t background:#006597; 
 
\t color:#fff; 
 
\t 
 
\t } 
 
\t .block-header th{ 
 
\t text-align:center; 
 
\t } 
 
\t .active{ 
 
\t background:#006597; 
 
\t color:#fff; 
 
\t } 
 
\t .addrow{ 
 
\t width:10%; 
 
\t height:100px; 
 
\t background:#006597; 
 
\t float:left; 
 
\t text-align:center; 
 
\t color:#fff; 
 
\t line-height:100px; 
 
\t cursor:pointer; 
 
\t word-wrap: break-word; 
 
\t overflow:hidden; 
 
\t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table style="width:45%; float:left;" id="table-txt"> 
 
     <tr height="25" class="block-header"> 
 
     <th width="25%">Name</th> 
 
     <th width="25%">Age</th> 
 
     <th width="25%">Gender</th> 
 
     <th width="25%">Salary</th> 
 
     </tr> 
 
     <tr height="25"> 
 
     <td><input type="text" style="width:100%;" id="empname" placeholder="EMP Name"/></td> 
 
     <td><select style="width:100%;" id="age"><option disabled selected>Select Age</option><option>20+</option></select></td> 
 
     <td><select style="width:100%;" id="gender"><option disabled selected>Select Gender</option><option>Male</option><option>Female</option></select></td> 
 
     <td><select style="width:100%;" id="salary"><option disabled selected>Select Salary</option><option>4LPM+</option><option>5LPM+</option></select></td> 
 
     </tr> 
 
     </table> 
 
\t <div class="addrow" id="getrow">GET RECORD</div> 
 
\t <table style="width:45%; float:right;" id="final"> 
 
     <tr height="25" class="block-header"> 
 
     <th width="25%">Name</th> 
 
     <th width="25%">Age</th> 
 
     <th width="25%">Gender</th> 
 
     <th width="25%">Salary</th> 
 
     </tr> 
 

 
     
 
     
 
     </table>

+0

总是会比空不同! –

+3

'.val()'永远不会返回null。 Protip:'console.log(a,typeof a)',并检查字段为空时你真正得到的值。 – JJJ

+0

你检查错误的东西。您的变量将始终为非空值,因为您将它们设置为“'。你应该检查是否(a!==''&& ...)。 – enguerranws

回答

0

a!=nul 变化

if (a.trim()) { 
    // is not empty or whitespace 
} 

$(document).ready(function(){ 
 
var a=''; 
 
var b=''; 
 
var c=''; 
 
var d=''; 
 
$('#getrow').click(function(){ 
 
a = $('#empname').val(); 
 
b = $('#age option:selected').val(); 
 
c = $('#gender option:selected').val(); 
 
d = $('#salary option:selected').val(); 
 

 
if(a.trim() && b.trim() && c.trim() && d.trim()){ 
 
    alert('Passed'); 
 
    //alert(a+" "+b+" "+c+" "+d); 
 
    $('#final').append('<tr height=\"25\"><td>'+a+'</td><td>'+b+'</td><td>'+c+'</td><td>'+d+'</td></tr>'); 
 

 
    $('#empname').val(' '); 
 
    $('#age').val(' '); 
 
    $('#gender').val(' '); 
 
    $('#salary').val(' '); 
 
} 
 
    else{ 
 
    alert('Failed'); 
 
    } 
 
    
 

 
}); 
 
});
table,tr,th,td{ 
 
     border:1px solid #dddddd; 
 
     border-collapse:collapse; 
 
    } 
 
\t .td-bg{ 
 
\t background:#006597; 
 
\t color:#fff; 
 
\t opacity:0.7; 
 
\t cursor:pointer; 
 
\t } 
 
\t .block-header{ 
 
\t background:#006597; 
 
\t color:#fff; 
 
\t 
 
\t } 
 
\t .block-header th{ 
 
\t text-align:center; 
 
\t } 
 
\t .active{ 
 
\t background:#006597; 
 
\t color:#fff; 
 
\t } 
 
\t .addrow{ 
 
\t width:10%; 
 
\t height:100px; 
 
\t background:#006597; 
 
\t float:left; 
 
\t text-align:center; 
 
\t color:#fff; 
 
\t line-height:100px; 
 
\t cursor:pointer; 
 
\t word-wrap: break-word; 
 
\t overflow:hidden; 
 
\t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table style="width:45%; float:left;" id="table-txt"> 
 
     <tr height="25" class="block-header"> 
 
     <th width="25%">Name</th> 
 
     <th width="25%">Age</th> 
 
     <th width="25%">Gender</th> 
 
     <th width="25%">Salary</th> 
 
     </tr> 
 
     <tr height="25"> 
 
     <td><input type="text" style="width:100%;" id="empname" placeholder="EMP Name"/></td> 
 
     <td><select style="width:100%;" id="age"><option disabled value='' selected>Select Age</option><option>20+</option></select></td> 
 
     <td><select style="width:100%;" id="gender"><option disabled value='' selected>Select Gender</option><option>Male</option><option>Female</option></select></td> 
 
     <td><select style="width:100%;" id="salary"><option disabled value='' selected>Select Salary</option><option>4LPM+</option><option>5LPM+</option></select></td> 
 
     </tr> 
 
     </table> 
 
\t <div class="addrow" id="getrow">GET RECORD</div> 
 
\t <table style="width:45%; float:right;" id="final"> 
 
     <tr height="25" class="block-header"> 
 
     <th width="25%">Name</th> 
 
     <th width="25%">Age</th> 
 
     <th width="25%">Gender</th> 
 
     <th width="25%">Salary</th> 
 
     </tr> 
 

 
     
 
     
 
     </table>

+0

它不工作检查只有在文本框中的值,然后点击获取其通过,但它是无效的 –

+0

请尝试知道请:)我修复它 –