編輯: OK,我相信我已經找到了使用@ManseUK貼有@約翰的評論沿着信息解決此問題的方法。作爲一個n00b我不能回答我自己的問題,但我已經在問題的下面添加了一個解釋,以幫助其他人解決問題。JavaScript函數給出錯誤
I am re-writing part of an e-commerce solution which was written by another development team some years ago. In the new version, we are experimenting with shortening the user journey using Ajax but doing so gives JavaScript errors and causes some functions to fail. Dev URL is here:
http://cognition.thelightbulb.co.uk/type-18/general-purpose-lamps-and-bulbs/craftlight-daylight
The errors appear once the dropdowns have been selected and the product displays.
The errors are displaying most notably in IE7:
Error: 'frm.qty' is null or not an object Error: 'qty.value' is null or not an object
I believe this is where the problem is coming from:
var frm = document.frmOrder; var qty = frm.qty;
In the lines above,
frmOrder
is the name of the form andqty
is the name of the input for product quantity.Compare that to http://cognition.thelightbulb.co.uk/product-54 where the product loads without the Ajax selection process and you'll see that the functions work correctly.
I suspect that the problem is to do with the fact that
var frm = document.frmOrder;
is not working due to the way it relates to the DOM when loaded with Ajax.I am using
innerHTML=xmlhttp.responseText
as the Ajax method. Is there an alternative way to definevar frm
so that it will function properly when loaded with Ajax?
編輯: 使用@ManseUK貼有@約翰的評論沿着信息,我添加了另一個參數CheckMinQty(minorder)
,以便它現在看起來像這樣...
function CheckMinQty(minorder,qty)
...其中qty
傳遞給onclick
事件的功能爲document.forms['frmOrder'].qty.value
然後我將整個功能移出一個單獨的.js文件。這可能不是最好的方法,但它仍然感覺Ajax調用返回可工作的HTML,其中CheckMinQty
可以使用,而不是引入整個負載<script>
,然後嘗試運行它。
感謝您提出的所有建議,並歡迎您對上述方法/解決方案提出任何意見。
感謝您的建議@ManseUK,但我仍然得到'錯誤:'frm.qty'爲null或不是一個對象'當頁面加載。 –