如何在 JavaScript 中动态设置对象的唯一标识符?

huangapple go评论83阅读模式
英文:

How to dynamicaly set up unique id to objects in js?

问题

在.NET项目中编写代码时,遇到了JavaScript的问题。

我需要一种方法来触发单击“生成”按钮,该按钮在单击后会获取来自下拉菜单的数据。

我有一个名为“btn”的按钮,当我单击它时,页面会加载一个带有“dropdown”按钮的图像。我需要在单击“Dropdown”按钮时获取下拉菜单中的数据(n、size、url),以便稍后使用,但是单击事件只读取第一个按钮的值。我尝试创建一个方法来创建唯一的按钮ID,但它不起作用,所有按钮都具有相同的ID。

var i = 0;
$('.dropdown').each(function () {
    i++;
    var newID = 'VarBtn' + i;
    $(this).attr('id', newID);
});

$(document).ready(() => {
    $('#btn').click(function () {
        var input = {};
        input.n = parseInt($('#quantity').val());
        input.prompt = $('#txt').val();
        input.size = $('#sel').find(":selected").val();
        $.ajax({
            url: '/Home/GenerateImage',
            method: 'post',
            contentType: 'application/json',
            data: JSON.stringify(input)            
        }).done(function (data) {
            $.each(data.data, function () {
                $('#display').append(                    
                    '<div class="col-md-3 p-10" style="padding-top:10px">' +
                    '<div class="dropdown">' +
                    '<button class="btnV" id="idOne">Dropdown</button>' +
                    '<div id="myDropdown" class="dropdown-content">' +
                    '<br>Select size' +
                    '<select id="Vsel">' +
                    '<option selected>256x256</option>' +
                    '<option>512x512</option>' +
                    '<option>1024x1024</option>' +
                    '</select>' +
                    '<br>Enter quantity' +
                    '<input type="number" id="Vquantity" value="1" min="1" max="10" />' +
                    '<br>' +
                    '<button id="VarBtn_' + i + '">Generate</button>' +
                    '</div>' +
                    '</div>' +
                    '<img class="p-10" id="GenImg" src="' + this.url + '">' +
                    '</div>');
            });
        });
    });

    $(document.body).on('click', '#VarBtn', function () {
        //Code..  
    });
});

我尝试为按钮创建唯一的ID,以便单击“VarBtn”按钮将访问所需的按钮,并通过下拉菜单的唯一ID获取必要的数据,但是所有ID仍然相同。

英文:

Writing a project in .Net and having trouble with js.

I need a method to trigger a click on the 'generate' button that takes data from the (dropdown menu) after the click.

I have a button 'btn', when I click on it the page loads an image with a 'dropdown' button. I need to take data from the dropdown menu(n, size, url) when I click on the button 'Dropdown' to use it later but the onclick method only reads the values of the first button. I tried to create a method to create unique button id but it doesn't work, all buttons have the same id.

var i = 0;
$(&#39;.dropdown&#39;).each(function () {
i++;
var newID = &#39;VarBtn&#39; + i;
$(this).attr(&#39;id&#39;, newID);
});
$(document).ready(() =&gt;
{
$(&#39;#btn&#39;).click(function ()
{
var input = {};
input.n = parseInt($(&#39;#quantity&#39;).val());
input.prompt = $(&#39;#txt&#39;).val();
input.size = $(&#39;#sel&#39;).find(&quot;:selected&quot;).val();
$.ajax({
url: &#39;/Home/GenerateImage&#39;,
method: &#39;post&#39;,
contentType: &#39;application/json&#39;,
data: JSON.stringify(input)            
}).done(function (data) {
$.each(data.data, function () {
$(&#39;#display&#39;).append(                    
&#39;&lt;div class=&quot;col-md-3 p-10&quot; style=&quot;padding-top:10px&quot;&gt;&#39; +
&#39;&lt;div class=&quot;dropdown&quot;&gt;&#39; +
&#39;&lt;button class=&quot;btnV&quot; id=&quot;idOne&quot;&gt;Dropdown&lt;/button&gt;&#39; +
&#39;&lt;div id=&quot;myDropdown&quot; class=&quot;dropdown-content&quot;&gt;&#39; +
&#39;&lt;br&gt;Select size&#39; +
&#39;&lt;select id=&quot;Vsel&quot;&gt;&#39; +
&#39;&lt;option selected&gt;256x256&lt;/option&gt;&#39; +
&#39;&lt;option&gt;512x512&lt;/option&gt;&#39; +
&#39;&lt;option&gt;1024x1024&lt;/option&gt;&#39; +
&#39;&lt;/select&gt;&#39; +
&#39;&lt;br&gt;Enter quantity&#39; +
&#39;&lt;input type=&quot;number&quot; id=&quot;Vquantity&quot; value=&quot;1&quot; min=&quot;1&quot; max=&quot;10&quot; /&gt;&#39; +
&#39;&lt;br&gt;&#39; +
&#39;&lt;button id=&quot;VarBtn_&#39;+i+ &#39;&quot; &gt;Generate&lt;/button&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;img class=&quot;p-10&quot; id=&quot;GenImg&quot; src = &quot;&#39; + this.url + &#39;&quot;&gt;&#39; +
&#39;&lt;/div&gt;&#39;);
});
});
});
$(document.body).on(&#39;click&#39;, &#39;#VarBtn&#39;, function () {
//Code..  
});

I tried to make unique id's for the buttons, so that clicking on the 'VarBtn' button will access the desired button and take the necessary data by unique id from the dropdown menu, but all id's remain the same.

答案1

得分: 2

以下是您提供的代码的翻译部分:

var i = 0;
$(&#39;.dropdown&#39;).each(function () {
    i++;
    var newID = &#39;VarBtn&#39; + i;
    $(this).attr(&#39;id&#39;, newID);
});

$(document).ready(() =&gt;
{
    $(&#39;#btn&#39;).click(function ()
    {
        var input = {};
        input.n = parseInt($(&#39;#quantity&#39;).val());
        input.prompt = $(&#39;#txt&#39;).val();
        input.size = $(&#39;#sel&#39;).find(&quot;:selected&quot;).val();
        $.ajax({
            url: &#39;/Home/GenerateImage&#39;,
            method: &#39;post&#39;,
            contentType: &#39;application/json&#39;,
            data: JSON.stringify(input)            

        }).done(function (data) {
            $.each(data.data, function () {
                var btnID = &#39;VarBtn_&#39; + i;
                $(&#39;#display&#39;).append(                    
                    &#39;&lt;div class=&quot;col-md-3 p-10&quot; style=&quot;padding-top:10px&quot;&gt;&#39; +
                    &#39;&lt;div class=&quot;dropdown&quot;&gt;&#39; +
                    &#39;&lt;button class=&quot;btnV&quot; id=&quot;idOne&quot;&gt;Dropdown&lt;/button&gt;&#39; +
                    &#39;&lt;div id=&quot;myDropdown&quot; class=&quot;dropdown-content&quot;&gt;&#39; +
                    &#39;&lt;br&gt;Select size&#39; +
                    &#39;&lt;select id=&quot;Vsel&quot;&gt;&#39; +
                    &#39;&lt;option selected&gt;256x256&lt;/option&gt;&#39; +
                    &#39;&lt;option&gt;512x512&lt;/option&gt;&#39; +
                    &#39;&lt;option&gt;1024x1024&lt;/option&gt;&#39; +
                    &#39;&lt;/select&gt;&#39; +
                    &#39;&lt;br&gt;Enter quantity&#39; +
                    &#39;&lt;input type=&quot;number&quot; id=&quot;Vquantity&quot; value=&quot;1&quot; min=&quot;1&quot; max=&quot;10&quot; /&gt;&#39; +
                    &#39;&lt;br&gt;&#39; +
                    &#39;&lt;button id=&quot;&#39; + btnID + &#39;&quot; &gt;Generate&lt;/button&gt;&#39; +
                    &#39;&lt;/div&gt;&#39; +
                    &#39;&lt;/div&gt;&#39; +
                    &#39;&lt;img class=&quot;p-10&quot; id=&quot;GenImg&quot; src = &quot;&#39; + this.url + &#39;&quot;&gt;&#39; +
                    &#39;&lt;/div&gt;&#39;);
            });
        });
    });

$(document.body).on(&#39;click&#39;, &#39;[id^=VarBtn]&#39;, function () {
    var dropdown = $(this).closest(&#39;.dropdown-content&#39;);
    var input = {};
    input.size = dropdown.find(&#39;#Vsel&#39;).val();
    input.quantity = parseInt(dropdown.find(&#39;#Vquantity&#39;).val());
    // do something with input
});

希望这有助于您理解代码。如果您有任何问题或需要进一步的帮助,请随时提问。

英文:

Try this :

var i = 0;
$(&#39;.dropdown&#39;).each(function () {
i++;
var newID = &#39;VarBtn&#39; + i;
$(this).attr(&#39;id&#39;, newID);
});
$(document).ready(() =&gt;
{
$(&#39;#btn&#39;).click(function ()
{
var input = {};
input.n = parseInt($(&#39;#quantity&#39;).val());
input.prompt = $(&#39;#txt&#39;).val();
input.size = $(&#39;#sel&#39;).find(&quot;:selected&quot;).val();
$.ajax({
url: &#39;/Home/GenerateImage&#39;,
method: &#39;post&#39;,
contentType: &#39;application/json&#39;,
data: JSON.stringify(input)            
}).done(function (data) {
$.each(data.data, function () {
var btnID = &#39;VarBtn_&#39; + i;
$(&#39;#display&#39;).append(                    
&#39;&lt;div class=&quot;col-md-3 p-10&quot; style=&quot;padding-top:10px&quot;&gt;&#39; +
&#39;&lt;div class=&quot;dropdown&quot;&gt;&#39; +
&#39;&lt;button class=&quot;btnV&quot; id=&quot;idOne&quot;&gt;Dropdown&lt;/button&gt;&#39; +
&#39;&lt;div id=&quot;myDropdown&quot; class=&quot;dropdown-content&quot;&gt;&#39; +
&#39;&lt;br&gt;Select size&#39; +
&#39;&lt;select id=&quot;Vsel&quot;&gt;&#39; +
&#39;&lt;option selected&gt;256x256&lt;/option&gt;&#39; +
&#39;&lt;option&gt;512x512&lt;/option&gt;&#39; +
&#39;&lt;option&gt;1024x1024&lt;/option&gt;&#39; +
&#39;&lt;/select&gt;&#39; +
&#39;&lt;br&gt;Enter quantity&#39; +
&#39;&lt;input type=&quot;number&quot; id=&quot;Vquantity&quot; value=&quot;1&quot; min=&quot;1&quot; max=&quot;10&quot; /&gt;&#39; +
&#39;&lt;br&gt;&#39; +
&#39;&lt;button id=&quot;&#39; + btnID + &#39;&quot; &gt;Generate&lt;/button&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;/div&gt;&#39; +
&#39;&lt;img class=&quot;p-10&quot; id=&quot;GenImg&quot; src = &quot;&#39; + this.url + &#39;&quot;&gt;&#39; +
&#39;&lt;/div&gt;&#39;);
});
});
});
$(document.body).on(&#39;click&#39;, &#39;[id^=VarBtn]&#39;, function () {
var dropdown = $(this).closest(&#39;.dropdown-content&#39;);
var input = {};
input.size = dropdown.find(&#39;#Vsel&#39;).val();
input.quantity = parseInt(dropdown.find(&#39;#Vquantity&#39;).val());
// do something with input
});

huangapple
  • 本文由 发表于 2023年2月24日 00:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定