简体   繁体   中英

How can i more effectively programming for this situation?

I'm a newbie in Jquery and i want to change this code more short and effetive.

This is my code in the below and this code check the checkbox and change the value. anyway anybody know about this jquery, please help me.

function fn_checkboxChange(){
    $("#redng_author_y").change(function(){
        if($(this).is(":checked")){
            $("#redng_author").val($(this).val());
        }
    })
    $("#redng_author_n").change(function(){
        if($(this).is(":checked")){
            $("#redng_author").val($(this).val());
        }
    })
    $("#redng_author_y").change(function(){
        if($(this).is(":checked")){
            $("#regist_author").val($(this).val());
        }
    })
    $("#updt_author_y").change(function(){
        if($(this).is(":checked")){
            $("#updt_author").val($(this).val());
        }
    })
    $("#updt_author_n").change(function(){
        if($(this).is(":checked")){
            $("#updt_author").val($(this).val());
        }
    })
    $("#delete_author_y").change(function(){
        if($(this).is(":checked")){
            $("#delete_author").val($(this).val());
        }
    })
    $("#delete_author_n").change(function(){
        if($(this).is(":checked")){
            $("#delete_author").val($(this).val());
        }
    })
}
function setChangeListener(elem, targetElem){
    $(elem).change(function(){
              if($(this).is(":checked")){
              $(targetElem)).val($(this).val());
             }
           });
     }
}

then call that function for all such sets:

function fn_checkboxChange(){
    setChangeListener( "#redng_author_y", "#redng_author");
    setChangeListener( "#redng_author_n", "#redng_author");
    setChangeListener( "#updt_author_y", "#updt_author");
    setChangeListener( "#updt_author_n", "#updt_author");
    setChangeListener( "#delete_author_y", "#delete_author");
    setChangeListener( "#delete_author_n", "#delete_author");
}

Or if frequently many checkboxes map to the same input, you could:

    function setChangeListener( targetElem, ...elems){
      elems.forEach(function(elem){

        $(elem).change(function(){
              if($(this).is(":checked")){
                $(targetElem)).val($(this).val());
             }
           });
     }); 
   }

and

function fn_checkboxChange(){
    setChangeListener(  "#redng_author", "#redng_author_y", "#redng_author_n");
    setChangeListener(  "#updt_author", "#updt_author_y","#updt_author_n");
    setChangeListener(  "#delete_author","#delete_author_y","#delete_author_n");
}

Add a class and a property to your checkboxes

HTML

<input type="checkbox" class="mycheckbox" data-target="someinput1" />
<input type="checkbox" class="mycheckbox" data-target="someinput2" />
<input type="checkbox" class="mycheckbox" data-target="someinput3" />
<input id="someinput1" ... />
<input id="someinput2" ... />
<input id="someinput3" ... />

JS

$(".mycheckbox").change(function(){
      if($(this).is(":checked")){
          $("#" + $(this).data("target")).val($(this).val());
      } 
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM