모델 데이터를 자바 스크립트 변수로 가져와 int 값으로 사용하여 값을 비교해야합니다. 하지만 모델 데이터를 문자열로 가져 오는 방법 만 알아낼 수 있습니다. 그렇지 않으면 컴파일러가 불평합니다.ASP.NET MVC 모델 데이터의 Javascript int 변수?
그래서 max 및 taskBudgetHours를 자바에서 int 변수로 가져올 수 있습니까?
<script type="text/javascript">
$(document).ready(function() {
$("#taskForm").submit(function (e) {
var taskBudgetHours = $('#BudgetHours').val();
var max = '<%: Model.Project.RemainingBudgetHours %>';
alert(taskBudgetHours);
alert(max);
if (taskBudgetHours <= max) { //This doesn't work, seems to treat it as strings...
return true;
}
else {
//Prevent the submit event and remain on the screen
alert('There are only ' + max + ' hours left of the project hours.');
return false;
}
});
});
</script>
코드를 약간 편집했습니다. 가장 주목할만한 것은'return false'를한다면'e.preventDefault()'를 할 필요가 없다는 것입니다. – Domenic