이 모든 것을 load() 본문 이벤트에 넣으면 페이지가 렌더링되고 DOM 객체가 만들어진 후에로드해야합니까? (나는 그것을 사용하기 위해 .value보다는 .innerhtml을 사용해야 만했다.) 그렇다면 어떻게 ...모든 자바 스크립트를 body 이벤트의 load() 안에 넣어야합니까?
(* 나는 이것이 쓰레기 코드라는 것을 알고 있지만 그것은 나의 마지막 시도보다 낫다. 내 다음 시도. 내가 컴백 시간이 좀되면 내부 함수를 가진 리터럴 생성자를 사용하여 다시 만들 것이다. 나는이 자바 스크립트를 더 이상 사용하지 않을 것이다. 백엔드 PHP는 보안과 체크를 처리 할 것이다.)
<script type="text/javascript">
//To get the price of a product applying discount
function getPrice(location,quantity)
{
//Get the product choice
var e = document.getElementById("productList["+location+"]");
var productSelected = e.options[e.selectedIndex].value;
//TODO: Determine discounts based on product choice
switch(productSelected)
{
case '0':
return 0;
case '1':
return 10;
case '2':
return 15;
}
return null;
}
//To update product only
function updateProduct(location)
{
updateRow(location,document.getElementById("quantity["+location+"]").value);
}
//To update only that row
function updateRow(location,quantity)
{
//Check Quantity is a valid Number and also not a float or negative
if (!isNaN(quantity) && parseFloat(quantity) && isFinite(quantity) && (quantity >= 0)) {
quantity = Math.floor(quantity);
} else {
quantity = 0;
};
//Update the quantity input field to whatever quantity is - Investigate later!
document.getElementById("quantity["+location+"]").value = quantity;
//Store old Price for changing total
var oldTotal = document.getElementById("totalPrice").innerHTML;
var oldLinePrice = document.getElementById("linePrice["+location+"]").innerHTML;
//Calculate and Store Prices to adjust the total
var productPrice = getPrice(location,quantity).toFixed(2);
var newLinePrice = (quantity*productPrice).toFixed(2);
//Apply Updates
document.getElementById("unitPrice["+location+"]").innerHTML = productPrice;
document.getElementById("linePrice["+location+"]").innerHTML = newLinePrice;
document.getElementById("totalPrice").innerHTML = (oldTotal-oldLinePrice+parseFloat(newLinePrice)).toFixed(2);
}
</script>
DOM을로드 한 후 실행해야하는 코드는로드 함수에 있어야합니다. 나는 이것이 동어체 간파라는 것을 알지만 다른 대답은 무엇을 기대합니까? – asawyer
이것들은 단지 함수 정의 일 뿐이므로 원하는 곳마다 코드에 넣을 수 있습니다. 페이지가로드 된 후 호출하는 것을 잊지 마십시오. – d4rkpr1nc3
@ d4rkpr1nc3 그게 뭔지는 모르겠지만 DOM을 요소로 사용하지 않아서 생각한 .value보다는 일부 장소에서만 .innerHTML을 사용할 수 있습니다. –