Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Jquery更改动态生成的html中的值_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用Jquery更改动态生成的html中的值

Javascript 使用Jquery更改动态生成的html中的值,javascript,jquery,html,Javascript,Jquery,Html,正如标题所说,我想知道是否有办法更改jquery生成的html项的值。以下是我目前拥有的代码: javascript文件 <script type="text/javascript"> $(document).ready(function() { $('#months').change(function() { var currentDate = new Date(); var curentMonth = currentDate.getMon

正如标题所说,我想知道是否有办法更改jquery生成的html项的值。以下是我目前拥有的代码:

javascript文件

<script type="text/javascript">

$(document).ready(function() {
    $('#months').change(function() {
        var currentDate = new Date();
        var curentMonth = currentDate.getMonth();
        var curentYear = currentDate.getYear();

        $(".row:not(:first)").remove();
        for (var i = 0; i < this.selectedIndex; i++)
        $(".row:first").clone(true).insertAfter(".row:last");
        $('#dateDueMonth['+i+']').val(curentMonth + 1);
    })
});

</script>

$(文档).ready(函数(){
$('#月')。更改(函数(){
var currentDate=新日期();
var currentMonth=currentDate.getMonth();
var currentyear=currentDate.getYear();
$(“.row:not(:first)”).remove();
对于(变量i=0;i
我在这里的想法是,在克隆行之后,我可以根据它的当前名称更改下拉列表内容,但显然这不起作用,而且我无法找到一种方法“钩住”动态html内容

和基本html

<select id="months" name="months">

<option selected value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>

<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
</td><div id="showRows"><div class="row" id="row[]"><td class="td_alt"><select id="dateDueMonth[]" name="dateDueMonth[]" >
<option value="0">Select</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>

<option value="5">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option selected value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>

 <select id="dateDueYear[]" name="dateDueYear[]">
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option selected value="2011">2011</option>
</select>

1.
2.
3.
4.
5.
6.
7.
8.
9
10
11
12
挑选
简
二月
破坏
四月
也许
六月
七月
八月
九月
十月
十一月
12月
2005
2006
2007
2008
2009
2010
2011
现在,动态html和表单提交的生成非常有效。提供可用于后端代码的数据。然而,拥有它,我可以在我认为合适的情况下增加各种下拉列表中的值,这将使使用此页面的人更容易输入数据,因此问题就出现了

对于所有这些回复,感谢您对jquery/jsnoob的帮助


编辑:为此,我想要的是一个表单,它可以将封装在showRows div中的第一行的副本spawm成倍地复制。提交该表单时,我希望它能够为生成的行的每个select/input语句收集数组中的信息。例如,如果我选择了两个月,dateDueMonth、dateDueYear和amount将是表单中的数组,每个数组将有两个条目。当前代码可以执行此操作,但默认情况下,下拉菜单的默认元素设置与它要克隆的HTML行相同。

不确定要执行什么操作,但它应该是这样工作的

$(document).ready(function() {
    $('#months').change(function() {
        var currentDate = new Date();
        var curentMonth = currentDate.getMonth();
        var curentYear = currentDate.getYear();
        var arr;
        $(".row:not(:first)").remove();
        for (var i = 0; i < this.selectedIndex; i++)
        arr=[$(".row:first").clone(true).insertAfter(".row:last")];
        $('#dateDueMonth['+i+']').val(curentMonth + 1); //don't know why its here

        arr[0].text("your value"); //apply the index you wanna change
    })
});
$(文档).ready(函数(){
$('#月')。更改(函数(){
var currentDate=新日期();
var currentMonth=currentDate.getMonth();
var currentyear=currentDate.getYear();
var-arr;
$(“.row:not(:first)”).remove();
对于(变量i=0;i
不确定您想做什么,但它应该是这样工作的

$(document).ready(function() {
    $('#months').change(function() {
        var currentDate = new Date();
        var curentMonth = currentDate.getMonth();
        var curentYear = currentDate.getYear();
        var arr;
        $(".row:not(:first)").remove();
        for (var i = 0; i < this.selectedIndex; i++)
        arr=[$(".row:first").clone(true).insertAfter(".row:last")];
        $('#dateDueMonth['+i+']').val(curentMonth + 1); //don't know why its here

        arr[0].text("your value"); //apply the index you wanna change
    })
});
$(文档).ready(函数(){
$('#月')。更改(函数(){
var currentDate=新日期();
var currentMonth=currentDate.getMonth();
var currentyear=currentDate.getYear();
var-arr;
$(“.row:not(:first)”).remove();
对于(变量i=0;i
在JavaScript中不能将ID用作数组。如果需要,在创建元素时也必须放置数组索引。或者只需访问下面的select元素

$(document).ready(function() {
    $('#months').change(function() {
        var currentDate = new Date();
        var curentMonth = currentDate.getMonth();
        var curentYear = currentDate.getYear();

        $(".row:not(:first)").remove();
        for (var i = 0; i < this.selectedIndex; i++) {
            $(".row:first").clone(true).insertAfter(".row:last");
            $('.row:last select').val(curentMonth + 1);
        }
    })
});
$(文档).ready(函数(){
$('#月')。更改(函数(){
var currentDate=新日期();
var currentMonth=currentDate.getMonth();
var currentyear=currentDate.getYear();
$(“.row:not(:first)”).remove();
对于(变量i=0;i

在JavaScript中不能将ID用作数组。如果需要,在创建元素时也必须放置数组索引。或者只需访问下面的select元素

$(document).ready(function() {
    $('#months').change(function() {
        var currentDate = new Date();
        var curentMonth = currentDate.getMonth();
        var curentYear = currentDate.getYear();

        $(".row:not(:first)").remove();
        for (var i = 0; i < this.selectedIndex; i++) {
            $(".row:first").clone(true).insertAfter(".row:last");
            $('.row:last select').val(curentMonth + 1);
        }
    })
});
$(文档).ready(函数(){
$('#月')。更改(函数(){
var currentDate=新日期();
var currentMonth=currentDate.getMonth();
var currentyear=currentDate.getYear();
$(“.row:not(:first)”).remove();
对于(变量i=0;i

您是否正在尝试实现这种标记


挑选
...
2005
...
挑选
...
2005
...
...

这可能是你需要的。请参阅上面链接的我的演示

for (var i = 1; i <= this.selectedIndex; i++) {
    row = $(".row:first").clone(true)[0];
    row.id = "row["+i+"]";
    $(row).find("[id^='dateDueMonth']").attr("id","dateDueMonth["+i+"]").val((curentMonth + i) % 12 + 1);
    $(row).find("[id^='dateDueYear']").attr("id","dateDueYear["+i+"]");
    $(row).insertAfter(".row:last");
}

for(var i=1;i您是否试图实现这种标记


挑选
...
2005
...
挑选
...
2005
...
...

这可能是你需要的。请参阅上面链接的我的演示

for (var i = 1; i <= this.selectedIndex; i++) {
    row = $(".row:first").clone(true)[0];
    row.id = "row["+i+"]";
    $(row).find("[id^='dateDueMonth']").attr("id","dateDueMonth["+i+"]").val((curentMonth + i) % 12 + 1);
    $(row).find("[id^='dateDueYear']").attr("id","dateDueYear["+i+"]");
    $(row).insertAfter(".row:last");
}

for(var i=1;我是来帮忙的,我真的很感谢你的描述和代码,但我不知道你在找什么:(你只是想让用户编辑这些元素吗?
$('dateduemount['+i+')).val(curentMonth+1)
从未执行过。您想要实现什么样的最终标记?我们当然可以帮助您,但您必须澄清您的问题。谈论您更大的目标将非常有帮助。您是否试图加载本月(28-31)的天数,然后更改名称[“天”]?当您使用诸如
if
for
之类的语句时。不要忘记使用
{}
。否则它可能会在格式化时中断您的代码。如果您不使用括号,它只会影响到下一行。@Chamika Sandamal眼睛很好。我以为我昨晚已经修复了,但假设没有。我是来帮忙的,我非常感谢您的描述和代码,但我不知道您在寻找什么:(您只是想让用户编辑这些元素吗?