簡體   English   中英

如何針對這種情況更有效地編程?

[英]How can i more effectively programming for this situation?

我是Jquery的新手,我想更簡短,更有效地更改此代碼。

這是下面的代碼,該代碼選中復選框並更改值。 無論如何有人知道這個jquery,請幫助我。

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());
             }
           });
     }
}

然后針對所有此類集合調用該函數:

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");
}

或者,如果經常有許多復選框映射到同一輸入,則可以:

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

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

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");
}

在您的復選框中添加類和屬性

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());
      } 
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM