在jquery .after()函数内部添加for循环 [英] add for loop inside jquery .after() function

查看:134
本文介绍了在jquery .after()函数内部添加for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jquery .after()函数内部使用for循环添加<option>标记. 我有以下代码:

I want to add <option> tag using for loop inside jquery .after() function. I have the following code:

$("#myid").after("<!--more tags here-->"+
                  "<select class='form-control' name='dropdownmenu'>"+
                  "for (i=0; i < 4 ; i++){"+
                  "<option>i</option>}"+
                  "</select>"+
                  "<!--more tags here-->")

但这不起作用,我得到的只是1个<option>标记,其中的字母i

But that doesn't work, all I get is 1 <option> tag with the letter i inside

推荐答案

您不能在js中循环.您可以执行以下操作.您可以先创建选择块,然后再附加它.代码未经测试,希望您能理解.

you can not loop in js like that. you can something like following. You can create your select block first and then append it. code not tested, hopefully you get the idea.

var test = "<select class='form-control' name='dropdownmenu'>";

for (i=0; i < 4 ; i++){
    test += "<option>"+i+"</option>}";                  
}
test += "</select>";

$("#myid").after(test);  // note: use . for class or # for id

您仍然可以通过这种方式添加更多标签:

you can still add more tags this way:

$("#myid").after("<span>start</span>"+test+"<span>end</span>");  

这篇关于在jquery .after()函数内部添加for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆