【问题标题】:Alart or modal screen javascript警报或模态屏幕javascript
【发布时间】:2015-01-28 13:42:02
【问题描述】:

我正在开发一个网站,其中有一部分我执行邮政编码查询,当我给出“TAB”或简单地单击该字段以通知数据到查询执行事件时(我正在执行我的 JavaScript我的输入框中的 onblur 事件中的代码)。但是,我需要在查询等待期间出现的“模态”屏幕或“警报”。

已经尝试使用下面的方法,但是我没有成功,不要在onblur事件中工作,而不是在onclick:

 $ ('# BuscaCEP') modal ('show.');
 $ ('# BuscaCEP') modal ('hide');.

有人会给小费吗?

我的通用密码:

 <li class="fields">
                    <div class="field">
                        <div class="form-group">
                            <?php //inputbox to the query from the onblur ?>
                            <label for="zip" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?>
                            </label>

                            <div class="input-box input-box-bkg-lage-small">

                                <input type="text" name="postcode"
                                       value="<?php echo $this->escapeHtml($this->getAddress()->getPostcode()) ?>"
                                       title="<?php echo $this->__('Zip/Postal Code') ?>" id="zip"
                                       onblur="consultacep(this.value)"
                                       OnKeyPress="formatNumber('#####-###', this, event)" maxlength="9"
                                       class="form-control validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>"/>

                            </div>
                        </div>

                    </div>

                </li>


<script type="text/javascript">

//Javascript Code
    function consultacep(cep) {

        cep = cep.replace(/\D/g, "")
        url = "http://cep.correiocontrol.com.br/" + cep + ".js"
        s = document.createElement('script')
        s.setAttribute('charset', 'utf-8')
        s.src = url
        document.querySelector('head').appendChild(s);


     //where the the loading screen should be performed
        $('#buscaCEP').modal('show');
    }

    function openWindow() {
        $('#buscaCEP').open();
    }

    function correiocontrolcep(valor) {
        if (valor.erro) {
            alert('Cep não encontrado');
            return;
        }


        var xmlhttp = new XMLHttpRequest();


        xmlhttp.onreadystatechange = function () {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                document.getElementById('Street').value = valor.logradouro
                document.getElementById('bairro').value = valor.bairro
                document.getElementById('city').value = valor.localidade
                document.getElementById("region_id").value = xmlhttp.responseText;

                //where the the loading screen should be terminated
                $('#buscaCEP').modal('hide');
            }
        }


        var url = "<?php echo $this->getBaseUrl()?>";
        xmlhttp.open("GET", url + "buscaUF.php?uf=" + valor.uf, true);
        xmlhttp.send();
    }

</script>

在 Leopoldo Negro 的帮助下,到达了以下情况:

模态屏幕:

    <!-- Modal  -->
            <div class="modal fade" id="buscaCEP" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                        </div>
                        <div class="modal-body">
                            Loading...
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>

输入框:

<li class="fields">
            <div class="field">
                <div class="form-group">
                    <?php //começa o cep e dados região ?>
                    <label for="zip" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?>
                    </label>

                    <div class="input-box input-box-bkg-lage-small">

                        <input type="text" name="postcode"
                               value="<?php echo $this->escapeHtml($this->getAddress()->getPostcode()) ?>"
                               title="<?php echo $this->__('Zip/Postal Code') ?>" id="zip"
                               onblur="consultacep(this.value)"
                               OnKeyPress="formatNumber('#####-###', this, event)" maxlength="9"
                               class="form-control validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>"/>

                    </div>
                </div>

            </div>

        </li>

调用事件:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
        <script type="text/javascript">

            $(document).ready(function() {


                $('input[name="zip"]').blur(function(){
                    consultacep(this.value);
                });
            });

        </script>

Javascript:

<script>
    function consultacep(cep) {

        cep = cep.replace(/\D/g, "")
        url = "http://cep.correiocontrol.com.br/" + cep + ".js"
        s = document.createElement('script')
        s.setAttribute('charset', 'utf-8')
        s.src = url
        document.querySelector('head').appendChild(s);

        $('#buscaCEP').modal('show');

    }

    function openWindow() {
        $('#buscaCEP').open();
    }

    function correiocontrolcep(valor) {
        if (valor.erro) {
            alert('Cep não encontrado');
            return;
        }


        var xmlhttp = new XMLHttpRequest();


        xmlhttp.onreadystatechange = function () {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                document.getElementById('Street').value = valor.logradouro
                document.getElementById('bairro').value = valor.bairro
                document.getElementById('city').value = valor.localidade
                /*document.getElementById('region_id').value=valor.uf*/
                /*document.getElementById('uf').value=valor.uf*/
                document.getElementById("region_id").value = xmlhttp.responseText;
                //where the the loading screen should be performed
                $('#buscaCEP').modal('hide');

            }
        }


        var url = "<?php echo $this->getBaseUrl()?>";
        xmlhttp.open("GET", url + "buscaUF.php?uf=" + valor.uf, true);
        xmlhttp.send();
    }
</script>

但仍然无法调出模态屏幕 =/

【问题讨论】:

    标签: javascript jquery twitter-bootstrap modal-dialog alert


    【解决方案1】:

    试试这个:

    $('#zip').blur(function(){
        consultacep(this.value);
    });
    

    这将以编程方式将 onblur 事件与 jQuery 绑定。

    注意:确保 id 为 buscaCEP 的项目存在。

    编辑:

    原始错误来自您定义函数的地方,当“onblur”事件触发时,回调未定义并触发错误。 尝试将您的 javascript 添加到网站的标题或使用我提供的等待 DOM 加载的方法。 不要重新定义模糊事件回调,只使用一种方法。 simple example

    【讨论】:

    • 谢谢,但这不是我想要的 :( 我需要的是在引用 CEP 时打开一个模态屏幕,当查询完成此模态时单独关闭它
    • 模态屏幕是指新窗口吗?还是只是当前网络中的一个对话框?
    • 只是当前网页中的一个对话框
    • 那么我的答案应该有效,因为您在 ajax 回调中调用 $('#buscaCEP').modal('show'); 和 consultacep() 和 $('#buscaCEP').modal('hide');。
    • 嘿 Leopoldo,我把代码贴在那里了,我想我应用了你的提示或理解错误或不起作用:/
    【解决方案2】:

    每个人都不能做上面建议的两种方法,然后做了一些基本的工作,我发帖是为了帮助别人!

    为弹出窗口创建了一个 div:

    <DIV id="popup">
                            <span style="color: #0871B5;font-size: 20px ">BUSCANDO CEP!</span>
                            <br/>
                            <img src="<?php echo str_replace('index.php/' ,'',$this->getBaseUrl() . 'skin/frontend/default/TW/Images_TW/loader.gif') ?>"/>
                        </DIV>
    

    并在javascript函数中打开它:

    <script>
    
        function fechar(){
            document.getElementById('popup').style.display = 'none';
        }
    
        function abrir(){
            document.getElementById('popup').style.display = 'block';
            setTimeout ("fechar()", 3000);
        }
    
    
        function consultacep(cep) {
            abrir();
            cep = cep.replace(/\D/g, "")
            url = "http://cep.correiocontrol.com.br/" + cep + ".js"
            s = document.createElement('script')
            s.setAttribute('charset', 'utf-8')
            s.src = url
            document.querySelector('head').appendChild(s);
    
        }
    
        function correiocontrolcep(valor) {
            if (valor.erro) {
                alert('Cep não encontrado');
                return;
            }
    
            var xmlhttp = new XMLHttpRequest();
    
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById('Street').value = valor.logradouro
                    document.getElementById('bairro').value = valor.bairro
                    document.getElementById('city').value = valor.localidade
                    /*document.getElementById('region_id').value=valor.uf*/
                    /*document.getElementById('uf').value=valor.uf*/
                    document.getElementById("region_id").value = xmlhttp.responseText;
                    fechar();
                }
            }
            var url = "<?php echo $this->getBaseUrl()?>";
            xmlhttp.open("GET", url + "buscaUF.php?uf=" + valor.uf, true);
            xmlhttp.send();
        }
    
    </script>
    

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2013-09-15
      • 2015-11-18
      • 2014-03-07
      • 2018-10-13
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多