【问题标题】:new input added in page not run script页面中添加的新输入未运行脚本
【发布时间】:2016-03-13 12:24:17
【问题描述】:

当我使用 (.load()) 重新加载 DIV 中的内容时,我的网站出现问题 单选按钮不运行 onchange 事件我该如何解决?

$(window).ready(function(){
                $('input[name="optionsRadios"]').on('change', function(){
                if ($(this).val()=='veloce') {                  
                    //change to "show update"   
                    var senzasped = <?php echo $tot; ?>;                        
                    senzasped += 5;                     
                    $("#prezzotot").text(senzasped);
                    $("#spedizionese").text("Spedizione Veloce");
                    $("#spedizionese2").text("5,00€");                  
                } else  {
                    var senzasped = <?php echo $tot; ?>;
                    senzasped += 3.5;                           
                    $("#prezzotot").text(senzasped);
                    $("#spedizionese").text("Spedizione Normale");
                    $("#spedizionese2").text("3,50€");
                }
            });
            });

HTML:

<div class="modal-sped" style="width:100%; height:100px">
                <div class="row">
                    <div class="col-md-12" style="margin-left:20px">
                        <label>Selezione la spedizione: </label>
                        <div class="radio">
                            <label>
                                <input type="radio" name="optionsRadios" id="optionsRadios1"value="normale" checked>
                                    Spedizione Normale 3,50€
                            </label>
                        </div>
                        <div class="radio">
                            <label>
                                <input type="radio" name="optionsRadios" id="optionsRadios2" value="veloce">
                                    Spedizione <strong>Veloce</strong> 5,00€
                            </label>
                        </div>
                    </div>
                </div>
            </div>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您需要将事件绑定到未更改的元素或document:

    $(document).on('change', 'input[name="optionsRadios"]', function() {
    

    这是因为您附加更改侦听器的元素被load 替换,并且新输入没有附加任何侦听器。如果您将侦听器附加到 document 或其他未被删除的父元素,它将适用于所有存在或将插入的匹配输入。

    【讨论】:

    • 谢谢它的工作!你能解释一下区别吗? :P 谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 2014-09-10
    相关资源
    最近更新 更多