javascript - 一個id 綁定click事件 做不同的條件判斷?

來源:互聯網
上載者:User

js代碼:

 //彈窗註冊協議    ms=window.parent.document.getElementById('inputzz1');    ks=window.parent.document.getElementById('inputzz');    $('#yes-agree').click(function(){        console.log(ms);        if(!(ms.getAttribute('checked'))){            console.log(123);            $("#inputzz1").attr("checked",true);            $("#check-do1").addClass("check-do-on");            $("button[title='註冊']").removeAttr("disabled","disabled").css("background-color","#E4393C");//啟用提交按鈕            console.log(1234);        }        if(!(ks.getAttribute('checked'))){            $("#inputzz").attr("checked",true);            $("#check-do").addClass("check-do-on");            $(".pt-mid").css({"background-position":"-57px -303px"},                    {"width":"175px"},{"height":"33px"});            $("#btnrdz").removeAttr('disabled','disabled');//禁用提交按鈕        }        $('.sc-model').fadeOut().html("");        $('.sc-zzc-bg').fadeOut();    });    $('#no-agree').click(function(){        if(ms.getAttribute('checked')){            console.log(123);            $("#inputzz1").attr("checked",false);            $("#check-do1").removeClass("check-do-on");            $("button[title='註冊']").attr('disabled','disabled').css("background-color","#ccc");//禁用提交按鈕        }        if(ks.getAttribute('checked')){            $("#inputzz").attr("checked",false);            $("#check-do").removeClass("check-do-on");            $(".pt-mid").css({"background-position":"-610px -583px"},                    {"width":"175px"},{"height":"33px"});            $("#btnrdz").attr('disabled','disabled');//禁用提交按鈕        }        $('.sc-model').fadeOut().html("");        $('.sc-zzc-bg').fadeOut();    });

觸發事件html片段:

      <我已閱讀國廣相關手冊>    

註冊協議html:

不同意 同 意

問題補充: 註冊協議這個彈窗html是放在外部的,並且頁面兩處地方點擊<我已閱讀國廣相關手冊>彈出的是同一個外部的html.
現在點擊同意或者不同意控制的是兩個form表單中的樣式。

需求: 分別點擊同意或者不同意這兩個id時,能否讓js只修改相應的form表單內的樣式??

回複內容:

js代碼:

 //彈窗註冊協議    ms=window.parent.document.getElementById('inputzz1');    ks=window.parent.document.getElementById('inputzz');    $('#yes-agree').click(function(){        console.log(ms);        if(!(ms.getAttribute('checked'))){            console.log(123);            $("#inputzz1").attr("checked",true);            $("#check-do1").addClass("check-do-on");            $("button[title='註冊']").removeAttr("disabled","disabled").css("background-color","#E4393C");//啟用提交按鈕            console.log(1234);        }        if(!(ks.getAttribute('checked'))){            $("#inputzz").attr("checked",true);            $("#check-do").addClass("check-do-on");            $(".pt-mid").css({"background-position":"-57px -303px"},                    {"width":"175px"},{"height":"33px"});            $("#btnrdz").removeAttr('disabled','disabled');//禁用提交按鈕        }        $('.sc-model').fadeOut().html("");        $('.sc-zzc-bg').fadeOut();    });    $('#no-agree').click(function(){        if(ms.getAttribute('checked')){            console.log(123);            $("#inputzz1").attr("checked",false);            $("#check-do1").removeClass("check-do-on");            $("button[title='註冊']").attr('disabled','disabled').css("background-color","#ccc");//禁用提交按鈕        }        if(ks.getAttribute('checked')){            $("#inputzz").attr("checked",false);            $("#check-do").removeClass("check-do-on");            $(".pt-mid").css({"background-position":"-610px -583px"},                    {"width":"175px"},{"height":"33px"});            $("#btnrdz").attr('disabled','disabled');//禁用提交按鈕        }        $('.sc-model').fadeOut().html("");        $('.sc-zzc-bg').fadeOut();    });

觸發事件html片段:

      <我已閱讀國廣相關手冊>    

註冊協議html:

不同意 同 意

問題補充: 註冊協議這個彈窗html是放在外部的,並且頁面兩處地方點擊<我已閱讀國廣相關手冊>彈出的是同一個外部的html.
現在點擊同意或者不同意控制的是兩個form表單中的樣式。

需求: 分別點擊同意或者不同意這兩個id時,能否讓js只修改相應的form表單內的樣式??

很簡單的東西,寫這麼複雜

$('#yes-agree').on('click', function(){    // 這裡判斷浮層是否顯示,假設$(xxx)是你的浮層    if($(xxx).is(':visible')){        // 浮層顯示,修改浮層內樣式        $('#inputzz1').prop('checked', true);        $("#check-do1").addClass("check-do-on");        $("button[title='註冊']").prop('disabled', false);    } else {        // 浮層隱藏,修改頁面內樣式        $("#inputzz").prop("checked",true);        $("#check-do").addClass("check-do-on");        $("btnrdz").prop('disabled', false);    }    $('.sc-model').fadeOut().html("");    $('.sc-zzc-bg').fadeOut();});

謝謝,你的提示,代碼是精簡了好多,不過我換了種方式去做了,通過改變id的名稱去做click事件的綁定,這樣兩個form的動作就不會衝突了。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.