简体   繁体   English

如何获取动态生成按钮的ID

[英]How to get the ID of dynamically generated buttons

I auto genrated some buttons 我自动生成了一些按钮

    <div class="wrapper" id="wrapper"></div>
<script type="application/javascript">
    var x, i, y;
    for(i = 0; i <= 10; i++){
        y = i;
        x = "<button class=btn_class"+y+"id=btn"+y+"onclick(alert(this.id);>Ukor</button>";
        $("#wrapper").append(x);
    }

    $("button").on("click", function(){
        alert(this.id);
    });
</script>

I want to be perform different action when any of the buttons are clicked. 我想在点击任何按钮时执行不同的操作。 but i can't seem to get get the buttons by id. 但我似乎无法通过id获取按钮。 I also need to send y as an arguement to the function 我还需要发送y作为函数的争论

The problem is that you have no spaces separating the attributes. 问题是你没有空格分隔属性。 So you're creating an element that looks like: 所以你要创建一个看起来像这样的元素:

<button class=btnclass0id=btn0onclick(alert(this.id);>Ukor</button>

So everything is going into the class attribute. 所以一切都进入了class属性。 You should put quotes around the attribute values, and spaces between the attributes. 您应该在属性值周围加上引号,并在属性之间添加空格。

There's also no need for the onclick attribute, since you're using $("button").on("click", ...) to do that. 也不需要onclick属性,因为你使用$("button").on("click", ...)来做到这一点。

So it should be: 所以它应该是:

x = "<button class='btn_class"+y+"' id='btn"+y+"'>Ukor</button>";

You can also use jQuery's object-oriented way to create elements: 您还可以使用jQuery的面向对象的方式来创建元素:

x = $("<button>", {
        "class": "btn_class" + y,
        "id": "btn"+y,
        text: "Ukor"
});

BTW, why do you put y in the class? 顺便说一句,为什么你把y放在课堂上? Usually the point of classes is to have all the similar elements have the same class, so you can address them all at once. 通常,类的要点是让所有相似的元素具有相同的类,因此您可以一次性解决所有这些问题。

It looks like you are building your HTML without the necessary spaces or punctuation between attributes, so your button HTML will look like this: 看起来您正在构建HTML而没有必要的空格或属性之间的标点符号,因此您的按钮HTML将如下所示:

<button class=btn_class0id=btn0onclick(alert(this.id);>Ukor</button>

This means there is no id attribute. 这意味着没有id属性。

Try fixing it up like this: 尝试修复它像这样:

x = '<button class="btn_class' + y + '" id="btn' + y + '" onclick="alert(this.id);">Ukor</button>';
$("#wrapper").append(x);

...or better yet, use the jQuery API to build your DOM: ...或者更好的是,使用jQuery API构建您的DOM:

x = $('<button onclick="alert(this.id);">Ukor</button>').attr({
  id: 'btn' + y,
  className: 'btn_class' + y
});

Your problem is in the string formatting your code will generate 您的问题在于代码将生成的字符串格式

<button class=btnclass0id=btn0onclick(alert(this.id);>Ukor</button>

and you need: 你需要:

<button class="btnclass0" id="btn0" onclick="alert(this.id);">Ukor</button>

 var x, i, y; for (i = 0; i <= 10; i++) { y = i; x = "<button class='btn_class" + y + "' id='btn" + y + "'>Ukor</button>"; $("#wrapper").append(x); } $("button").on("click", function() { alert(this.id); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper" id="wrapper"></div> 

Add the content into a single string and then append it once into the dom - faster than appending each iteration. 将内容添加到单个字符串中,然后将其附加到dom中一次 - 比附加每次迭代更快。 Then using a designated click event handler - alert the id of the button. 然后使用指定的单击事件处理程序 - 提醒按钮的ID。 And the classes need to be a common value - not unique like the id's. 这些类需要是一个共同的价值 - 不像id那样独特。 Also - you don't need "y" at all and if you need to send the number to a function you have it already in the id - just get the numerical portion of the id and you can use it as required. 此外 - 你根本不需要“y”,如果你需要将数字发送到你已经在id中的函数 - 只需获取id的数字部分,你就可以根据需要使用它。

  $(document).ready(function(){ var x=""; for(i = 0; i <= 10; i++){ x += "<button class='btn_class' id='btn"+i+"'>Ukor</button> "; } $("#wrapper").append(x); $(document).on('click','.btn_class', function(){ (alert($(this).attr('id'))) }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper" id="wrapper"></div> 

I recommend to use String template to avoid errors of spacing between attrs on concatenating -> Better in visibility of variables in String: 我建议使用String模板来避免串联时attrs之间的间距错误 - >更好地了解String中变量的可见性:

 $(Array.from({length:11},(v,k)=>k) .map((i)=>`<button class="btn_class${i}" id="btn${i}" onclick="alert(this.id)">Ukor${i}</button>`) .join('')).appendTo('#wrapper') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper" id="wrapper"></div> 

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

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