在页面中添加新输入未运行脚本

new input added in page not run script

本文关键字:输入 运行 脚本 添加 新输入      更新时间:2023-09-26

当我使用 (.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>
您需要

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

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

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