我需要將模型數據轉換爲javascript變量,並將其用作int來比較值。但我只能弄清楚如何將模型數據作爲字符串來獲取,否則編譯器會抱怨。來自ASP.NET MVC模型數據的Javascript int變量?
那麼我怎樣才能得到最大和taskBudgetHours作爲int變量在Javascript中?
<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>
我編輯你的代碼了一下;最值得注意的是,如果你做'返回false',你不需要做`e.preventDefault()` – Domenic 2011-01-11 23:11:46