I am trying to get the value from a drop down list in JavaScript and HTML which are two dates ( the format is just year), but I have an error of say " TypeError: "#sortDateFrom".val is not a function "
I want to check if the beginning is smaller then the end.
MY HTML code:
<div class="col-lg-2 col-md-2 col-sm-2">
<select class="form-control" id="sortDateFrom">
<option selected disabled>-Date From-</option>
<option>1900</option>
<option>1910</option>
<option>1920</option>
<option>1930</option>
<option>1940</option>
<option>1950</option>
<option>1960</option>
<option>1970</option>
<option>1980</option>
<option>1990</option>
<option>2000</option>
<option>2010</option>
</select>
</div> <!-- end column 3: Date From -->
<div class="col-lg-2 col-md-2 col-sm-2">
<select class="form-control" id="sortDateTo">
<option selected disabled>-Date To-</option>
<option>1900</option>
<option>1910</option>
<option>1920</option>
<option>1930</option>
<option>1940</option>
<option>1950</option>
<option>1960</option>
<option>1970</option>
<option>1980</option>
<option>1990</option>
<option>2000</option>
<option>2010</option>
</select>
</div> <!-- end column 3: Date To -->
And my Function in Javascript:
<script>
$(document).ready(function(){
var startDate = ('#sortDateFrom').val();
var endDate = ('#sortDateTo').val();
if (startDate < endDate){
window.alert("The beginning date must be smaller then the end date ");
}
});
</script>
答案 0 :(得分:5)
你忘记了jquery $
您还需要在页面准备就绪时检查值何时更改
<script>
$(document).ready(function(){
var startDate = $('#sortDateFrom').val();
var endDate = $('#sortDateTo').val();
$('#sortDateFrom, #sortDateTo').change(function(){
if (startDate < endDate){
window.alert("The beginning date must be smaller then the end date ");
})
}
});
//
</script>
答案 1 :(得分:0)
看看这个:
您未在DEBUG=express:application node .
必须通过选项:选择。请注意你不要在选项中使用价值。为此,您使用.text()而不是.val()。
$('#sortDateFrom option:selected').text()
&#13;
$(function() {
$('#sortDateFrom, #sortDateTo').change(function() {
var startDate = $('#sortDateFrom option:selected').text();
var endDate = $('#sortDateTo option:selected').text();
if (startDate < endDate) {
console.log("The beginning date must be smaller then the end date ");
}
});
});
&#13;