[英]What's the best way to do javascript validation on ASP.Net page with Masterpage?
[英]ASP.NET MasterPage + Javascript Error
將ASP.NET Web表單(v3.5)轉換為使用母版頁后,我開始收到一個奇怪的Javascript錯誤。
內容頁面上有一塊JavaScript。 這里是:
<script type="text/javascript">
var attCount = 0;
function CreateAttachmentControl() {
var newAtt = document.createElement('span');
var newAttName = 'area' + attCount;
newAtt.setAttribute('id', newAttName);
newAtt.setAttribute('name', newAttName);
var newInput = document.createElement('input');
var newInputName = 'att' + attCount;
newInput.setAttribute('type', 'file');
newInput.setAttribute('id', newInputName);
newInput.setAttribute('name', newInputName);
if (newInput.addEventListener) {
newInput.addEventListener("onchange", CreateAttachmentControl, false);
} else if (newInput.attachEvent) {
newInput.attachEvent("onchange", CreateAttachmentControl);
} else {
newInput.onchange = CreateAttachmentControl;
}
var newRemove = document.createElement('a');
newRemove.setAttribute('href', 'javascript:RemoveAttachmentControl("' + attCount + '")');
newRemove.setAttribute('title', 'Remove this attachment');
newRemove.innerHTML = 'X';
newAtt.appendChild(newInput);
newAtt.appendChild(document.createTextNode(' '));
newAtt.appendChild(newRemove);
newAtt.appendChild(document.createElement('br'));
attArea.appendChild(newAtt); // error here
attCount++;
}
function RemoveAttachmentControl(n) {
// get input element
var input = document.getElementById('att' + n);
// if the input is blank dont delete
if (input.value != '' && input.value != null) {
var att = document.getElementById('area' + n);
attArea.removeChild(att);
}
}
</script>
錯誤是:“ attArea”未定義
但是,我知道不是,因為在我的JavaScript塊下面是這樣的:
...<td align="left" colspan="2" style="height: 13px" id="attArea" name="attArea"></td>...
在將Webform轉換為帶有母版頁的內容頁之前,這種方法非常有效。 是否存在一些已知的Javascript + Masterpage問題?
謝謝
在您提供的代碼示例中,attArea 是未定義的。 對其的第一個引用是調用attArea.appendChild()
。 是否在您未提供的來源中聲明了更高的位置?
我認為您不見了:
var attArea = document.getElementById('attArea');
attArea.appendChild(newAtt); // no more error!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.