在JavaScript中使用這種構造有什麼限制。在我的代碼後面我有財產,並得到它有相當複雜的邏輯。它調用其他方法等等,儘管在調試時它顯示返回良好的值,但我得到的結果是空字符串。我想知道我能做什麼,因爲這種類型的代碼在渲染方法中運行。JavaScript <%= MyProperty%>
0
A
回答
2
使用該方法呈現JavaScript沒有任何限制。你只需要確保你的屬性可以在各種場景下使用,比如標準的HTTP獲取,回傳等等。當然,你必須確保該屬性被正確地轉義/按照需要進行了JavaScript編碼,即如果它中包含引號的字符串值等
0
我用這一切的時候...
我也建議做如下:
this.txtProjectName = $('#<%=this.txtProjectName.ClientID %>');
...這樣txtProjectName
是一個jQuery對象從一開始。
下面是一個廣泛的例子:
請注意一些在下面的類中調用的是這裏沒有顯示的對象。
<script type="text/javascript">
var projectDialog = (function($) {
var publicInstances = {};
// ***********************
// form
publicInstances.form = form;
function form(controller) {
/// <summary></summary>
var self = this;
/// <summary></summary>
this.controller = controller;
/// <summary></summary>
this.validationController = null;
this.hidAsOf = $('#<%=this.hidAsOf.ClientID %>');
this.hidProjectKey = $('#<%=this.hidProjectKey.ClientID %>');
this.hidMode = $('#<%=this.hidMode.ClientID %>');
this.ddlClients = $('#<%=this.ddlClients.ClientID %>');
this.txtProjectName = $('#<%=this.txtProjectName.ClientID %>');
this.txtProjectNumber = $('#<%=this.txtProjectNumber.ClientID %>');
this.txtProjectManager = $('#<%=this.txtProjectManager.ClientID %>');
this.txtProjectDescription = $('#<%=this.txtProjectDescription.ClientID %>');
this.ddlEMREProductivityCurve = $('#<%=this.ddlEMREProductivityCurve.ClientID %>');
this.txtStartDate = $('#<%=this.txtStartDate.ClientID %>');
this.txtEndDate = $('#<%=this.txtEndDate.ClientID %>');
this.txtEngineeringHours = $('#<%=this.txtEngineeringHours.ClientID %>');
this.txtDirectHours = $('#<%=this.txtDirectHours.ClientID %>');
this.txtIndirectHours = $('#<%=this.txtIndirectHours.ClientID %>');
this.txtOtherHours = $('#<%=this.txtOtherHours.ClientID %>');
this.txtARRThreshold = $('#<%=this.txtARRThreshold.ClientID %>');
this.txtAppropriationHours = $('#<%=this.txtAppropriationHours.ClientID %>');
this.txtAppropriationDollars = $('#<%=this.txtAppropriationDollars.ClientID %>');
this.ibtnSaveProject = $('#<%=this.ibtnSaveProject.ClientID %>');
this.imgClose = $('#<%=imgClose.ClientID %>');
this.imgStartDate = $('#<%=imgStartDate.ClientID %>');
this.imgEndDate = $('#<%=imgEndDate.ClientID %>');
this.disabledCssClass = "textbox-locked";
/// <summary></summary>
this.initialize = function() {
var context = $(self.controller.container);
var contentArea = $(self.controller.contentArea, context);
self.validationController = new validation.controller(contentArea);
if(self.controller.readOnly == true)
self.disableForm();
/// <summary>Set defaults.</summary>
self.ddlClients.attr('disabled', 'disabled');
/// <summary>Register click events.</summary>
self.ibtnSaveProject.click(function() { self.controller.save(); });
self.imgClose.click(function() { self.close(); });
/// <summary>Register rounders.</summary>
$('input.rounder-decimal', context).numericRounder();
/// <summary>Register keypress events.</summary>
$('input.rounder-decimal', context).keypress(function(e) {
return enforceValueAsDollar(e, this.val);
});
/// <summary>Register change events.</summary>
$('input.rounder-decimal', context).change(function() {
var element = $(this);
var value = element.extractValue();
var formatted = $.formatNumber(value.toString(), { format: "#,###.00", locale: "us" });
if (isNaN(value))
formatted = 0;
element.val(formatted);
if (!element.is('.textbox-total'))
self.calculateAppropriationHours();
});
};
/// <summary></summary>
this.disableStartDate = function(){
self.imgStartDate.remove();
self.txtStartDate.removeClass(self.disabledCssClass);
self.txtStartDate.addClass('TextBoxLocked');
};
/// <summary></summary>
this.disableForm = function() {
self.disableStartDate();
self.ddlEMREProductivityCurve.attr('disabled', true);
self.txtProjectName.attr('disabled', true);
self.txtProjectNumber.attr('disabled', true);
self.txtProjectManager.attr('disabled', true);
self.txtProjectDescription.attr('disabled', true);
self.txtEndDate.attr('disabled', true);
self.txtEngineeringHours.attr('disabled', true);
self.txtDirectHours.attr('disabled', true);
self.txtIndirectHours.attr('disabled', true);
self.txtOtherHours.attr('disabled', true);
self.txtARRThreshold.attr('disabled', true);
self.txtAppropriationHours.attr('disabled', true);
self.txtAppropriationDollars.attr('disabled', true);
self.txtProjectName.addClass(self.disabledCssClass);
self.txtProjectNumber.addClass(self.disabledCssClass);
self.txtProjectManager.addClass(self.disabledCssClass);
self.txtProjectDescription.addClass(self.disabledCssClass);
self.txtEndDate.addClass(self.disabledCssClass);
self.txtEngineeringHours.addClass(self.disabledCssClass);
self.txtDirectHours.addClass(self.disabledCssClass);
self.txtIndirectHours.addClass(self.disabledCssClass);
self.txtOtherHours.addClass(self.disabledCssClass);
self.txtARRThreshold.addClass(self.disabledCssClass);
self.txtAppropriationHours.addClass(self.disabledCssClass);
self.txtAppropriationDollars.addClass(self.disabledCssClass);
self.ibtnSaveProject.css('display', 'none');
};
/// <summary></summary>
this.close = function() {
self.clear();
closeSubForm('<%=divProjectDialog.ClientID %>');
};
/// <summary></summary>
this.clear = function() {
self.validationController.clear();
self.hidProjectKey.val('0');
self.txtProjectName.val('');
self.txtProjectNumber.val('');
self.txtProjectManager.val('');
self.txtProjectDescription.val('');
self.txtStartDate.val('');
self.txtEndDate.val('');
self.txtEngineeringHours.val('0.00');
self.txtDirectHours.val('0.00');
self.txtIndirectHours.val('0.00');
self.txtOtherHours.val('0.00');
self.txtARRThreshold.val('0.00');
self.txtAppropriationHours.val('0.00');
self.txtAppropriationDollars.val('0.00');
};
/// <summary></summary>
this.isOpen = function() {
var context = $(self.controller.container)
return (context.css('opacity') == '0') ? false : true;
};
/// <summary></summary>
this.isValid = function() {
self.validationController.clear();
var key = parseInt(self.hidProjectKey.val());
if (self.ddlClients.val().length == 0) {
var validator = self.validationController.getValidator(self.ddlClients.id());
validator.appendMessage("Please choose an option.");
}
if (self.txtProjectName.val().length == 0) {
var validator = self.validationController.getValidator(self.txtProjectName.id());
validator.appendMessage("Cannot be empty.");
}
if (self.hidProjectKey.val().length > 0) {
var key = parseInt(self.hidProjectKey.extractValue());
if (key > 0)
{
if (self.ddlEMREProductivityCurve.val().toLowerCase() == 'none') {
var validator = self.validationController.getValidator(self.ddlEMREProductivityCurve.id());
validator.appendMessage("Please choose an option.");
}
if (self.txtStartDate.val().length == 0) {
var validator = self.validationController.getValidator(self.txtStartDate.id());
validator.appendMessage("Click calendar to choose a valid date.");
}
if (self.txtEndDate.val().length == 0) {
var validator = self.validationController.getValidator(self.txtEndDate.id());
validator.appendMessage("Click calendar to choose a valid date.");
}
if ((self.txtStartDate.val().length > 0) && (self.txtEndDate.val().length > 0)) {
var startDate = new Date(self.txtStartDate.val());
var endDate = new Date(self.txtEndDate.val());
if (startDate > endDate) {
var validator = self.validationController.getValidator(self.txtEndDate.id());
validator.appendMessage("End date must br greater than start date.");
}
}
}
}
if (self.txtARRThreshold.val().length == 0) {
var validator = self.validationController.getValidator(self.txtARRThreshold.id());
validator.appendMessage("Cannot be empty.");
}
else if (isNaN(self.txtARRThreshold.extractValue())) {
var validator = self.validationController.getValidator(self.txtARRThreshold.id());
validator.appendMessage("Is not a valid number.");
}
if (self.txtEngineeringHours.val().length == 0) {
var validator = self.validationController.getValidator(self.txtEngineeringHours.id());
validator.appendMessage("Cannot be empty.");
}
else if (isNaN(self.txtEngineeringHours.extractValue())) {
var validator = self.validationController.getValidator(self.txtEngineeringHours.id());
validator.appendMessage("Is not a valid number.");
}
if (self.txtDirectHours.val().length == 0) {
var validator = self.validationController.getValidator(self.txtDirectHours.id());
validator.appendMessage("Cannot be empty.");
}
else if (isNaN(self.txtDirectHours.extractValue())) {
var validator = self.validationController.getValidator(self.txtDirectHours.id());
validator.appendMessage("Is not a valid number.");
}
if (self.txtIndirectHours.val().length == 0) {
var validator = self.validationController.getValidator(self.txtIndirectHours.id());
validator.appendMessage("Cannot be empty.");
}
else if (isNaN(self.txtIndirectHours.extractValue())) {
var validator = self.validationController.getValidator(self.txtIndirectHours.id());
validator.appendMessage("Is not a valid number.");
}
if (self.txtOtherHours.val().length == 0) {
var validator = self.validationController.getValidator(self.txtOtherHours.id());
validator.appendMessage("Cannot be empty.");
}
else if (isNaN(self.txtOtherHours.extractValue())) {
var validator = self.validationController.getValidator(self.txtOtherHours.id());
validator.appendMessage("Is not a valid number.");
}
if (self.txtAppropriationHours.val().length == 0) {
var validator = self.validationController.getValidator(self.txtAppropriationHours.id());
validator.appendMessage("Cannot be empty.");
}
else if (isNaN(self.txtAppropriationHours.extractValue())) {
var validator = self.validationController.getValidator(self.txtAppropriationHours.id());
validator.appendMessage("Is not a valid number.");
}
if (self.txtAppropriationDollars.val().length == 0) {
var validator = self.validationController.getValidator(self.txtAppropriationDollars.id());
validator.appendMessage("Cannot be empty.");
}
else if (isNaN(self.txtAppropriationDollars.extractValue())) {
var validator = self.validationController.getValidator(self.txtAppropriationDollars.id());
validator.appendMessage("Is not a valid number.");
}
self.validationController.displayAll();
return (self.validationController.hasErrors() == true) ? false : true;
};
/// <summary></summary>
this.calculateAppropriationHours = function() {
var engineeringHours = (+self.txtEngineeringHours.extractValue()) || 0;
var directHours = (+self.txtDirectHours.extractValue()) || 0;
var indirectHours = (+self.txtIndirectHours.extractValue()) || 0;
var otherHours = (+self.txtOtherHours.extractValue()) || 0;
var total = engineeringHours + directHours + indirectHours + otherHours;
self.txtAppropriationHours.val(total).change();
};
/// <summary></summary>
this.populateFrom = function(project) {
self.clear();
self.hidProjectKey.val(project.key);
// Drop-Down
self.ddlClients.val(project.clientKey);
self.txtProjectName.val(project.projectName);
self.txtProjectNumber.val(project.number);
self.txtProjectManager.val(project.projectManager);
self.txtProjectDescription.val(project.description);
// Drop-Down
self.ddlEMREProductivityCurve.val(project.productivityCurveType);
if(project.startDate != null)
self.txtStartDate.val(project.startDate);
if(project.startDate != null)
self.txtEndDate.val(project.endDate);
self.txtEngineeringHours.val(project.engineeringHours);
self.txtDirectHours.val(project.directHours);
self.txtIndirectHours.val(project.indirectHours);
self.txtOtherHours.val(project.otherHours);
self.txtARRThreshold.val(project.arrThreshold);
self.txtAppropriationHours.val(project.appropriationHours);
self.txtAppropriationDollars.val(project.appropriationAmount);
self.txtEngineeringHours.change();
self.txtDirectHours.change();
self.txtIndirectHours.change();
self.txtOtherHours.change();
self.txtARRThreshold.change();
self.txtAppropriationHours.change();
self.txtAppropriationDollars.change();
};
};
return publicInstances;
})(jQuery);
</script>
相關問題
- 1. var-> myProperty和var.myProperty之間的區別
- 2. 替換&amp;&,<lt < and > gt gt to gt在javascript中
- 3. 更改<href>至<script> javascript?
- 4. 打<audio> + <video>從Javascript
- 5. 的Javascript運營商<< and >>
- 6. 像</p> <pre><code>Component { id: movie_delegate MovieListItem { movie: movie } } </code></pre> <p>內的JavaScript
- 7. 什麼是<script type =「text/javascript」><!--mce:0--></script>?
- 8. Javascript-onchange <option>
- 9. Javascript getElementById <img>
- 10. <textarea>用JavaScript
- 11. Objective-C屬性 - 正在使用[self myProperty]或self.myProperty比myProperty慢?
- 12. <script></script> or <script />?
- 13. iPhone的內存管理:</p> <pre><code>- (void)dealloc { [myProperty release]; [myProperty2 release]; } - (void)main { // // Parse operation undertaken here // self.myProperty = nil; self.myProperty2 = nil; } </code></pre> <p>我的想法是:設置self.someProperty =零
- 14. return Json(字典<int><int>) - > javascript
- 15. 使<b></b><i></i><u>在文本框</u>和<br>
- 16. 如何<li><a></a></li>
- 17. JavaScript和DOM:爲什麼「<table>」+ =「<tr></td></tr>」+ =「</ table」不起作用?
- 18. 浮動元素<br /><br /><br /><br /><br /\><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
- 19. Javascript不工作<a>
- 20. 的JavaScript <form>驗證
- 21. Javascript中<th>元素
- 22. 跨域javascript <-> php
- 23. SyntaxHighlighter(javascript)問題<del>
- 24. 的JavaScript/<text area >
- 25. JavaScript中的<body>
- 26. JavaScript函數的<asp:Buttoncolumn>
- 27. JavaScript <!-- //-->是必填項?
- 28. 的Javascript:<select>項目
- 29. Javascript匹配last @<User>
- 30. Javascript創建<img>
+1。做這件事時總會有一件事 - 當值是一個字符串時,忘記將我的<%= %>塊用引號括起來。看起來像我這樣做至少有一次。 – Matt 2011-05-24 18:02:01