본문 바로가기

잼있냐JavaScript/유용한 자바스크립트 UTIL

checkbox radio select 유용한 스크립트 7

/*
 * checkbox object checked 속성 체크
 */
function all_check(obj, true_or_false)
{
    if (obj)
    {
        if (obj.length)
        {
            for (var i=0; i<obj.length; i++)
                obj[i].checked = true_or_false;
        }
        else
            obj.checked = true_or_false;
    }
}

/*
 * radio, checkbox object checked=true count
 */
function checked_count(obj)
{
    var count = 0;

    if (!obj)
        return false;

    if (obj.length)
    {
        for (var i=0; i<obj.length; i++)
        {
            if (obj[i].checked)
                count++;
        }
    }
    else
    {
        if (obj.checked)
            count++;
    }

    return count;
}

/*
 * radio, checkbox object 체크 여부
 */
function is_checked(obj)
{
    return checked_count(obj)>0;
}

/*
 * radio object checked=true value
 */
function get_radio_value(obj)
{
    if (!obj)
        return null;

    if (obj.length)
    {
        for (var i=0; i<obj.length; i++)
        {
            if (obj[i].checked)
                return obj[i].value;
        }
    }
    else
    {
        if (obj.checked)
            return obj.value;
    }

    return null;
}

/*
 * select box value
 */
function get_select_value(obj)
{
    if (!obj)
        return null;

    return obj.options[obj.selectedIndex].value;
}

/*
 * select box all option selected 속성 체크
 */
function all_selected(obj, true_or_false)
{
    for (var i=0; i<obj.length; i++)
        obj.options[i].selected = true_or_false;
}

/*
 * select box object selected=true count
 */
function selected_count(obj)
{
    var count = 0;

    for (var i=0; i<obj.options.length; i++)
    {
        if (obj.options[i].selected)
            count++;
    }

    return count;
}

/*
 * add select box option object
 */
function add_select_box(obj, txt, value , select_value)
{
    var optItem = new Option();

    optItem.text = txt;
    optItem.value = value;

    if(select_value == value)
        optItem.selected = true;

    obj.options[obj.options.length] = optItem;


/*
 * remove select box list
 */
function remove_select_box(obj)
{
    for(var i=0; i<obj.options.length; i++)
    {
        obj.options[i].value = null;
        obj.options[i].text = null;
    }

    obj.options.length = 0;
}

/*
 * html tag object의 수를 반환
 */
function obj_length(obj)
{
    if (!obj)
        return 0;

    if (obj.length)
        return obj.length;
    else
        return 1;
}
-- 기타
/*입력 글자 길이 체크*/
function checkMaxSize(obj, num, objName)
{
    var Ppoint;
    var Tvalue = trim(obj.value.replace(/\r\n/g, "<br>"));
    var len = Tvalue.length;
    var cutLen = 0;
    var textlen = 0;
    var enter = 0;
    for(i=0;i < len; i++){
        Ppoint = obj.value.charAt(i);
        if (escape(Ppoint).length > 4) { //한글일 경우 3byte
            textlen +=3;
        } else {
            textlen++;
        } 
        if (textlen <= num) {
            cutLen = i + 1;
        }
    }
    if(textlen > num)
    {
        alert("Please input "+ objName +" less then "+ num+" character(Korean : "+ parseInt(String(num/3)) +" character)!!");
        //obj.value = Tvalue.substr(0, cutLen);
        return false;
    } 
    return true;
}