2014-12-19 199 views

回答

3

使用條件$('#first').val() || $('#second').val(),像這樣:

$('body').on('click', 'button', function() { 
    if ($('#first').val() || $('#second').val()) { // will fail if both are '' 
     alert("At least one has data"); 
    } else { 
     alert("Oops! No data"); 
    } 
}); 

DEMO

+0

感謝您的回答。這是我正在尋找的。 – Rahul 2014-12-19 07:18:25

1

嘗試這樣的:使用trim從開始和結束或僅空間安全。

$('body').on('click', 'button', function() { 
 
    if ($('#first').val().trim().length > 0 || $('#second').val().trim().length > 0) { 
 
     alert("we have some data"); 
 
    } else { 
 
     alert(" No data entered"); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
First name: <input type="text" name="FirstName" id="first"><br> 
 
Last name: <input type="text" name="LastName" id="second"><br> 
 

 
<button type="submit" value="Submit">submit</button>

相關問題