2017-06-12 3 views
-1

Ich habe eine dynamische Schaltfläche erstellt. Der Wert aller Schaltflächen ist jedoch gleich. Was soll ich dafür tun? Ich möchte, dass jeder verschiedene Werte hat.dynamische Schaltfläche mit jquery

Example image

for (var i = 0; i <= 11; i++) { 
 
    var r = $('<input type="button" value="new button" style="margin-left:10px; margin-top:5px;"/>'); 
 
    $("#sth").append(r); 
 

 
    $("#sth").css({ 
 
    width: '300px', 
 
    'padding-top': '10px', 
 
    'padding-bottom': '10px', 
 
    'padding-left': '10px' 
 
    }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<div id="sth"></div>

+0

so was Namen wollen Sie –

+2

'value = "neue Schaltfläche '+ i +'"' – mplungjan

+0

Was sollten diejenigen Name sein? Einfach 'Neue Taste 1',' Neue Taste2', ...? – Weedoze

Antwort

1

Sie können Index verwenden, um den Wert jeder Taste zu ändern. Sehen Sie, ob das hilft.

for (var i = 0; i <= 11; i++) { 
 
     var r = $('<input type="button" value="new button ' + i + '" style="margin-left:10px; margin-top:5px;"/>'); 
 
     $("#sth").append(r); 
 
} 
 
$("#sth").css({ width: '300px', 'padding-top': '10px', 'padding-bottom': '10px', 'padding-left':'10px' });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div id='sth'></div>

+1

Es ist klar, bereits in den Kommentaren auf eine Frage nicht zu antworten beantwortet – mplungjan

+0

Buddy, ich habe nicht überprüft, Kommentar, wenn ich anfange, diese Frage zu beantworten. Mein Bildschirm befand sich im Code-Modus und konnte daher nicht wissen, ob er beantwortet wurde oder nicht. Ich weiß es zu schätzen, dass du vor mir geantwortet hast. –

+0

Sie wissen, dass Sie in jeder Iteration das CSS des CSS setzen? – mplungjan

-1

sollten Sie die Zeichenfolge ändern:

$('<input type="button" value="new button" style="margin-left:10px; margin-top:5px;"/>'); 

Für etwas wie folgt aus:

$('<input type="button" value="new button-' + i + '" style="margin-left:10px; margin-top:5px;"/>'); 

So haben Sie: neue Taste-1, neue Taste-2 .... Hope that help

0

Wenn Sie nur nur unterschiedliche Namen setzen müssen, da nur Sie mit dem Index spielen

for (var i = 0; i <= 11; i++) { 
var r = $('<input type="button" value="Button_"' + i + '"style="margin-left:10px; margin-top:5px;"/>'); 
$("#sth").append(r); 

    $("#sth").css({ 
    width: '300px', 
    'padding-top': '10px', 
    'padding-bottom': '10px', 
    'padding-left': '10px' 
    }); 
} 

es so enter image description here

führen oder sonst können Sie Verwenden Sie ein benutzerdefiniertes Array, um einige Namen mit den entsprechenden Indizes

zu behalten
var arr = ['apple','orange','pineapple','tomato','grapes','ginger','ghee','sugar','milk','brinjal','potato'],name; 
for (var i = 0; i <= 11; i++) { 
    name = arr[i] ? arr[i] : 'button_'+ i; 
    var r = $('<input type="button" value="' + name + '"style="margin-left:10px; margin-top:5px;"/>'); 
    $("#sth").append(r); 

     $("#sth").css({ 
     width: '300px', 
     'padding-top': '10px', 
     'padding-bottom': '10px', 
     'padding-left': '10px' 
     }); 
    } 

das Ergebnis wird so sein, über enter image description here

+0

Klicken Sie auf das '<>' und erstellen Sie ein [mcve] - verschieben Sie das CSS auch außerhalb der Schleife – mplungjan