javascript null값 처리 질문좀요..!
본문
선택옵션에서 옵션을 선택후에 장바구니로 값을 넘기는 부분을 하고있는데요.
관리자 모드에서 상품에 선택옵션을 주면 괜찮은데, 선택옵션을 안주면
Uncaught TypeError: Cannot read property 'options' of null(…)
이렇게 오류가 나옵니다.
*스크립트 부분 입니다.
function fitem_submit(f)
{
var select = document.getElementById("it_option_1");
var select_val = select.options[select.selectedIndex].value;
var text = select.options[select.selectedIndex].text;
if (document.pressed == "장바구니") {
f.sw_direct.value = 0;
if(select_val.length == 0){
alert("옵션을 선택해주세요.");
return false;
}else{
f.text;
alert(text);
}
} else { // 바로구매
f.sw_direct.value = 1;
f.text;
}
옵션값이 null값이라 생기는 오류같은데 선택옵션이 없는 상품을 처리하려면 어떻게 처리해야할까요..?
답변 2
var objSelect = document.goodForm.selectName;
if (typeof(objSelect) != "undefined") {
var select_val = objSelect[objSelect.selectedIndex].value;
블라블라
}
자체 해결했습니다.
function fitem_submit(f)
{
var select = document.getElementById("it_option_1");
if(select != null){
var sellength = select.length;
var select_val = select.options[select.selectedIndex].value;
var text = select.options[select.selectedIndex].text;
if (document.pressed == "장바구니") {
f.sw_direct.value = 0;
if(select_val.length == 0){
alert("옵션을 선택해주세요.");
return false;
}else{
f.text;
alert(text);
}
} else { // 바로구매
f.sw_direct.value = 1;
f.text;
}
}else{
if (document.pressed == "장바구니") {
f.sw_direct.value = 0;
} else { // 바로구매
f.sw_direct.value = 1;
}
}