• Das Erstellen neuer Accounts wurde ausgesetzt. Bei berechtigtem Interesse bitte Kontaktaufnahme über die üblichen Wege. Beste Grüße der Admin

Funktion bei "Button" klick ausführen

EvilBen

New member
Huhu Leute,

ich hab nen kleines Problemchen. (wer hätte das gedacht ;) ) - Anfänger Problem halt:

Ich habe 4 Select boxen, die mit Auswahlmöglichkeiten gefüllt werden, je nachdem welches Item man auswählt. Zur Veranschaulichung mal der Code (bitte wunder euch nicht über die Item Values (11, 12 ....) ich bin ja noch am basteln, später dann soll der Code hinzugefügt werden.

Code:
<select class="auswahl" size="5" name="combo0" id="combo_0" onChange="change(this);">
	<option value="value1">Auswählen:</option>
	<option value="value2">1</option>
	<option value="value3">2</option>

</select>
<select class="auswahl" size="5" name="combo1" id="combo_1" onChange="change(this);">
	<option value="value1">  </option>
</select>
<select class="auswahl" size="5" name="combo2" id="combo_2" onChange="change(this);">
	<option value="value1">  </option>
</select><form action="" name="comb" method="post" enctype="multipart/form-data">
<select class="auswahl" size="5" name="combo3" id="combo_3" onChange="change(this);" onClick="return chkFormular()">
	<option value="value1">  </option>

</select>

JS dazu:

Code:
	data_1 = new Option("1", "$");
	data_2 = new Option("2", "$$");

// second combo box

	data_1_1 = new Option("11", "-");
	data_1_2 = new Option("12", "-");
	data_2_1 = new Option("21", "--");
	data_2_2 = new Option("22", "--");
	data_2_3 = new Option("23", "--");
	data_2_4 = new Option("24", "--");
	data_2_5 = new Option("25", "--");

// third combo box

	data_1_1_1 = new Option("111", "*");
	data_1_1_2 = new Option("112", "*");
	data_1_1_3 = new Option("113", "*");
	data_1_2_1 = new Option("121", "*");
	data_1_2_2 = new Option("122", "*");
	data_1_2_3 = new Option("123", "*");
	data_1_2_4 = new Option("124", "*");
	data_2_1_1 = new Option("211", "**");
	data_2_1_2 = new Option("212", "**");
	data_2_2_1 = new Option("221", "**");
	data_2_2_2 = new Option("222", "**");
	data_2_3_1 = new Option("231", "***");
	data_2_3_2 = new Option("232", "***");

// fourth combo box

	data_2_2_1_1 = new Option("2211","%")
	data_2_2_1_2 = new Option("2212","%%")

// other parameters

    displaywhenempty=""
    valuewhenempty=-1

    displaywhennotempty="Auswählen:"
    valuewhennotempty=0


function change(currentbox) {
	numb = currentbox.id.split("_");
	currentbox = numb[1];

    i=parseInt(currentbox)+1

// I empty all combo boxes following the current one

    while ((eval("typeof(document.getElementById(\"combo_"+i+"\"))!='undefined'")) &&
           (document.getElementById("combo_"+i)!=null)) {
         son = document.getElementById("combo_"+i);
	     // I empty all options except the first one (it isn't allowed)
	     for (m=son.options.length-1;m>0;m--) son.options[m]=null;
	     // I reset the first option
	     son.options[0]=new Option(displaywhenempty,valuewhenempty)
	     i=i+1
    }


// now I create the string with the "base" name ("stringa"), ie. "data_1_0"
// to which I'll add _0,_1,_2,_3 etc to obtain the name of the combo box to fill

    stringa='data'
    i=0
    while ((eval("typeof(document.getElementById(\"combo_"+i+"\"))!='undefined'")) &&
           (document.getElementById("combo_"+i)!=null)) {
           eval("stringa=stringa+'_'+document.getElementById(\"combo_"+i+"\").selectedIndex")
           if (i==currentbox) break;
           i=i+1
    }


// filling the "son" combo (if exists)

    following=parseInt(currentbox)+1

    if ((eval("typeof(document.getElementById(\"combo_"+following+"\"))!='undefined'")) &&
       (document.getElementById("combo_"+following)!=null)) {
       son = document.getElementById("combo_"+following);
       stringa=stringa+"_"
       i=0
       while ((eval("typeof("+stringa+i+")!='undefined'")) || (i==0)) {

       // if there are no options, I empty the first option of the "son" combo
	   // otherwise I put "-select-" in it

	   	  if ((i==0) && eval("typeof("+stringa+"0)=='undefined'"))
	   	      if (eval("typeof("+stringa+"1)=='undefined'"))
	   	         eval("son.options[0]=new Option(displaywhenempty,valuewhenempty)")
	   	      else
	             eval("son.options[0]=new Option(displaywhennotempty,valuewhennotempty)")
	      else
              eval("son.options["+i+"]=new Option("+stringa+i+".text,"+stringa+i+".value)")
	      i=i+1
	   }
       //son.focus()
       i=1
       combostatus=''
       cstatus=stringa.split("_")
       while (cstatus[i]!=null) {
          combostatus=combostatus+cstatus[i]
          i=i+1
          }
       return combostatus;
    }
}

Funktioniert einwandfrei, beim anklicken eines Items in der letzten Box wird eine JS Funktion ausgeführt die ein PHP Script ausführt dass dann den DB Eintrag erstellt (den Part hab ich oben weggelassen), und eine DIV unter den Select Boxen aktualisiert, die den gespeicherten Item anzeigt. Durch anklicken des Eintrages soll der Eintrag wieder gelöscht werden, und genau da liegt mein Problem. Zuerst einmal der Code:

Code:
.
.
.
$sql="SELECT * FROM  `users_profile_vorlieben` WHERE  `usr_profile_vorlieben_user` =$ip";
$r=$db->query($sql);
$anzahl=$r->num_rows;


if($anzahl > 0){
	?>
    <form action="" name="del" method="post" enctype="multipart/form-data">
    <?php
while($row=$r->fetch_assoc()){
	?>
<button id="a" name="a" type="button"  class="btn btn-default btn-sm" value="a"><?php echo $row['usr_profile_vorlieben_vorliebe']; ?>
  <a onClick="Test()"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> </a>
</button><input type="hidden" name="hidden" value="Test">
<?php
}}
?>
</form>

Jetzt habe ich Versucht den Button mit der ID "a" und auch dem Value = "a" abzufragen, nur leider funktioniert das nicht.

Code:
function Test(){
	test=document.del.a.value;


	window.alert("sometext  "+test);

}

habe das onClick Ereignis schon oben im Formular gehabt, und im "button" Element, hat nichts gebracht die Ausgabe bringt nur "sometext undefined".

Mir ist auch klar, dass die Buttons (das können ja bis zu 70 Stück sein) auch alle irgendwie eine eindeutige ID bekommen müssen um jeden Button einzeln abfragen zu können. Und da hänge ich jetzt irgendwie fest. Also Meine Frage ist:

Wie schaffe ich es, dass "undefined" durch den Value ersetzt wird ;)
Die ID des Buttons oder des "Hidden Fields" kriege ich schon an das PHP Script übergeben um für jeden Button die gewünschte Funktion zu bekommen.


Danke euch !!!
 
du kannst den buttons zb ein data-type mitgeben und dann die buttons via jquery each() durchlaufen
 
Warum hast du das onclick auf dem <a>, das irgendwie sinnfrei ist, und nicht direkt auf dem Button? Dann könntest du direkt onclick="test(this.value)" schreiben...
 
Zurück
Oben