简体   繁体   English

彩盒内的jQuery无法正常工作

[英]jQuery inside colorbox not working

I have an issue with colorbox content and can't figure out what to do with it. 我对颜色框的内容有疑问,无法弄清楚该怎么做。 and also, I'm not very familiar with jQuery. 而且,我对jQuery不太熟悉。

I have a Colorbox which displays the usual html content (it is not iframe-d). 我有一个Colorbox显示通常的html内容(不是iframe-d)。 There I have a little script for calculation depended on checkbox selection. 在那里,我有一个用于计算的小脚本,具体取决于复选框的选择。 This code works fine on a static page, but not working inside Colorbox. 这段代码可以在静态页面上正常工作,但不能在Colorbox中使用。 I read about $.colorbox.element() but didn't get it.. 我读到有关$ .colorbox.element()的信息,但没有得到..

Here's my part of code, any help is highly appreciated 这是我的代码部分,任何帮助都将受到高度赞赏

<script type="text/javascript"><!--
$('[id^=acc]').click(function() {
    if($(this).attr("checked")) {
        $('‪#‎total‬').text(parseInt($('#total').text()) + parseInt($(this).data('amount')));
        $('‪#‎monthly‬').text(parseInt($('#monthly').text()) + parseInt($(this).data('monthly')));
    } else {
        $('#total').text(parseInt($('#total').text()) - parseInt($(this).data('amount')));
        $('#monthly').text(parseInt($('#monthly').text()) - parseInt($(this).data('monthly')));
    } 
}); 
//--></script>

<span id="total">0</span> AZN
<span id="monthly">0</span> AZN
<input type="checkbox" class="accCheck" id="acc1" name="accessory[]" data-amount="100" data-monthly="15"/>
<label for="acc1"><span></span>Add to credit</label>

This is the part of code that calling form to be printed inside colorbox 这是调用表单要在colorbox中打印的代码的一部分

<script type="text/javascript"><!--
$(function() { 
    $("[id^=onlineCredit]").submit(function() {
        $.post($(this).attr("action"), $(this).serialize(), function(data) {
            $.colorbox({html:data});
        },
        'html');
        return false;
    });
});
//--></script>

page1.html page1.html

<span id="total">0</span> AZN
<span id="monthly">0</span> AZN
<input type="checkbox" class="accCheck" id="acc1" name="accessory[]" data-amount="100" data-monthly="15"/>
<label for="acc1"><span></span>Add to credit</label>

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">
        $('#acc1').click(function() {
            if($(this).attr("checked")) {
                $('#‎total').text(parseInt($('#total').text()) + parseInt($(this).data('amount')));
                $('#‎monthly').text(parseInt($('#monthly').text()) + parseInt($(this).data('monthly')));
            } else {
                $('#total').text(parseInt($('#total').text()) - parseInt($(this).data('amount')));
                $('#monthly').text(parseInt($('#monthly').text()) - parseInt($(this).data('monthly')));
            } 
        });
    </script>

page2.html page2.html

<form class="ajax" href="page1.html"> 
     <input  type="submit"/>
</form>

<link href="http://www.jacklmoore.com/colorbox/example1/colorbox.css" rel="stylesheet"></link>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://www.jacklmoore.com/colorbox/jquery.colorbox.js"></script
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(function() {
        $(".ajax").colorbox({width:"50%"});
    });
</script>

try this :) 尝试这个 :)

Try adding all the script to the page you are displaying the colorbox on. 尝试将所有脚本添加到要在其上显示colorbox的页面上。

also look for the use of prop and attr as attr("checked") was undefined in my browser. 还要查找propattr的使用,因为在我的浏览器中未定义attr("checked")

for example 例如

<script>
    $(document).ready(function(){

        $('[id^=acc]').click(function() {

        alert($(this).attr("checked")); -- this doesn't work on my browser
        alert($(this).prop("checked")); -- this does.

        if($(this).attr("checked")) {
            $('‪#‎total‬').text(parseInt($('#total').text()) + parseInt($(this).data('amount')));
            $('‪#‎monthly‬').text(parseInt($('#monthly').text()) + parseInt($(this).data('monthly')));
        } else {
            $('#total').text(parseInt($('#total').text()) - parseInt($(this).data('amount')));
            $('#monthly').text(parseInt($('#monthly').text()) - parseInt($(this).data('monthly')));
        } 
    });         

       $(function() { 
            $("[id^=onlineCredit]").submit(function() {
                $.post($(this).attr("action"), $(this).serialize(), function(data) {
                    $.colorbox({html:data});
                },
                'html');
                return false;
             });
    });
<\script>

other solution with form in same page 同一页面中具有表单的其他解决方案

<a id="inline" href="#myForm">myForm</a>
<form id="myForm" style="display:none">
    <span id="total">0</span> AZN
    <span id="monthly">0</span> AZN
    <input type="checkbox" class="accCheck" id="acc1" name="accessory[]" data-amount="100" data-monthly="15"/>
    <label for="acc1"><span></span>Add to credit</label>
    <input  type="submit"/>
</form>
<link href="http://www.jacklmoore.com/colorbox/example1/colorbox.css" rel="stylesheet"></link>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://www.jacklmoore.com/colorbox/jquery.colorbox.js"></script
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(function() {
        $("#inline").click(function(){
            $("#myForm").css("display","inline");
        }).colorbox({inline:true, href:"#myForm",width:"50%",
                        onClosed:function(){
                                    $("#myForm").css("display","none");
                                }
                    });
    });
    $('#acc1').click(function() {
        if($(this).attr("checked")) {
            $('#‎total').text(parseInt($('#total').text()) + parseInt($(this).data('amount')));
            $('#‎monthly').text(parseInt($('#monthly').text()) + parseInt($(this).data('monthly')));
        } else {
            $('#total').text(parseInt($('#total').text()) - parseInt($(this).data('amount')));
            $('#monthly').text(parseInt($('#monthly').text()) - parseInt($(this).data('monthly')));
        } 
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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