Po wpisaniu czegoś w danych wejściowych i kliknięciu przycisku ".val()" zwraca pustą wartość, każdy pomysł dlaczego?jQuery.val() nie działa na znaczniku <input>
kod HTML:
<div class="mainClass">
<div class="class1" >
<div class="class2">
<h3>Settings 1</h3>
<label>
<span>Text 1:</span>
<input type="text" name="text">
</label>
<label>
<span>Text 2:</span>
<input type="text" name="text2">
</label>
</div>
</div>
<div class="class3" >
<div class="class4">
<h3>Settings 2</h3>
<label>
<span>Text 1:</span>
<input type="text" name="text">
</label>
<label>
<span>Text 2:</span>
<input type="text" name="text2">
</label>
</div>
</div>
</div>
<button id="Go" class="go" >GO </button>
kod JavaScript:
$('#Go').on('click', function() {
alert($('.class1 input').val()); //return an empty value
$('.class1 input').val("test");
alert($('.class1 input').val()); //return test
});
EDIT:
Nawet tego nie robi praca, a tu mam tylko jedno wejście, więc nie mogę wpisać w źle:
<div class="mainClass">
<div class="class1" >
<div class="class2">
<h3>Settings 1</h3>
<label>
<span>Text 1:</span>
<input type="text" name="text">
</label>
</div>
</div>
</div>
<button id="Go" class="go" >GO </button>
EDIT 2: Znalazłem początek problemu ...
Kiedy robię, że:
$('.class1 input').each(function() {
alert($(this).attr('name') + " value:" +$(this).val());
});
mam dwa "wpis" tak:
text value:
text value:myinputText
Więc dwa utworzony na przykład w moim kodzie HTML, pierwszy jest pusty, a drugi działa dobrze! Przyglądając się uważnie mojej stronie, stwierdziłem, że wszystkie elementy są zduplikowane (<select,field, input...
)
Każdy pomysł, jak to możliwe? Czy dzwonię dwa razy do mojego pliku html? I cały mój kod jest w okienko (nie wiem, czy to może pomóc) (jestem nowy w JavaScript i jQuery)
Dzięki
'' nie jest etykietą .. –
@dystroy Dlaczego nie powinno? Jestem prawie pewien, że jest to całkowicie poprawne i zaakceptowane, aby to zrobić – Ian
@dystroy Oto referencja: http://stackoverflow.com/questions/774054/should-i-put-input-tag-inside-label-tag – Ian