繁体   English   中英

jQuery生成的选择选项不适用于动态字段

[英]jQuery generated select option not working on dynamic fields

我有两个静态选择字段,jQuery脚本在下拉菜单中附加了年份作为选项,而动态字段类似于前两个字段,您可以在单击“ weiter”链接时添加前两个字段。

现在,用于年份值附加的脚本在静态的前两个字段上效果很好,但不适用于动态生成的字段,即使我使用与前两个字段相同的类“ von_bis”来调用它们。

我想这是因为它们在文件加载时不存在。

这是当前情况: http : //jsfiddle.net/dzorz/PnRnR/

HTML:

<span class="label-f">von:</span>
<select class="span2 von_bis" id="von" name="von">
    <option value="0">von</option>
</select>
<span class="label-f">bis:</span>
<select class="span2 von_bis" id="bis" name="bis">
    <option value="0">bis</option>
</select>

<div id="yearWrapper">
</div>

<a href="" id="btn_weitere" class="btn_weitere">weitere</a>

脚本://年

$(function(){
    for (i = new Date().getFullYear(); i > 1900; i--)
    {
        $('.von_bis').append($('<option/>').val(i).html(i));
    }
});
//dinamic
$(document).ready(function() {

        var MaxInputs       = 5; //maximum input boxes allowed
        var InputsWrapper   = $("#yearWrapper"); //Input boxes wrapper ID
        var AddButton       = $("#btn_weitere"); //Add button ID

        var x = InputsWrapper.length; //initlal text box count
        var FieldCount=1; //to keep track of text box added

       $(AddButton).click(function (e)  //on add input button click
            {
                if(x <= MaxInputs) //max input box allowed
                {
                    FieldCount++; //text box added increment
                    //add input box
                    $(InputsWrapper).append('\
                    <div class="form-inline f-i-f-d">\
                        <div class="form-inline f-i-f-d">\
                            <select class="span2 von_bis" id="von'+ FieldCount +'"\
                            name="von'+ FieldCount +'">\
                            <option value="0">von</option>\
                            </select>\
                            <select class="span2 von_bis" id="bis'+ FieldCount +'"\
                            name="bis'+ FieldCount +'">\
                            <option value="0">bis</option>\
                            </select>\
                        </div>\
                        <a href="#" class="removeclass">remove</a>\
                    </div>');
                    x++; //text box increment
                }
                return false;
                });

                $("body").on("click",".removeclass", function(e){ //user click on remove text
                if( x > 1 ) {
                        $(this).parent('div').remove(); //remove text box
                        x--; //decrement textbox
                }
                return false;
                })

        });

如何将其应用于这些动态字段?

一种快速的方法是将$(function...)当前正在调用的代码移动到$(document).ready()的函数中。 定义函数后,对其进行调用,以便初始选择菜单将应用日期。 然后,在调用x++;之前,在if(x <= MaxInputs)语句中x++; ,再次调用该函数。 您可以在此处查看修复程序: http : //jsfiddle.net/PnRnR/1/

问题是,每次添加新复选框时都需要调用该函数时,您仅在加载时调用了该函数一次。

暂无
暂无

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

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