Wie der Titel sagt, bekomme ich "undefined", wenn ich versuche, das ID-Attribut eines Elements zu bekommen, im Grunde, was ich tun möchte, ist ein Element zu ersetzen mit einem Eingabefeld, wenn der Wert "Other" ist. Hier
ist der Code:
function showHideOther(obj){
var sel = obj.options[obj.selectedIndex].value;
var ID = $(this).attr("id");
alert(ID);
if(sel=='other'){
$(this).html("<input type='text' name='" + ID + "' id='" + ID + "' />");
}else{
$(this).css({'display' : 'none'});
}
}
Die HTML:
<span class='left'><label for='race'>Race: </label></span>
<span class='right'><select name='race' id='race' onchange='showHideOther(this);'>
<option>Select one</option>
<option>one</option>
<option>two</option>
<option>three</option>
<option value="other">Other</option>
</select>
</span>
Es ist wahrscheinlich etwas klein, dass ich nicht zu bemerken, was mache ich falsch?
Vielen Dank im Voraus!
"Dies" bezieht sich immer auf das Objekt, an dem Sie arbeiten. Und es kann überall verwendet werden. Natürlich, wenn Sie wissen, was Sie tun. – GerManson
anstelle von '$ (this) .href', könnte man einfach' this.href' und auch 'obj.id' ... btw,' obj.attr ('id'); 'sollte' $ (obj) sein .attr ('id'); 'basierend auf OP ... – Reigel