Pobierz wartość w polu tekstowym wprowadzania
Jakie są sposoby uzyskania i renderowania wartości wejściowej za pomocą jQuery?
Oto jeden:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
$("#txt_name").keyup(function(){
alert($(this).val());
});
})
</script>
<input type="text" id="txt_name" />
9 answers
//Get
var bla = $('#txt_name').val();
//Set
$('#txt_name').val(bla);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-31 08:10:33
Możesz wybrać wartość tylko na dwa sposoby:
// First way to get a value
value = $("#txt_name").val();
// Second way to get a value
value = $("#txt_name").attr('value');
Jeśli chcesz użyć prostego JavaScript, aby uzyskać wartość, oto jak:
document.getElementById('txt_name').value
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-13 18:39:11
Jest jedna ważna rzecz, o której należy wspomnieć:
$("#txt_name").val();
Zwróci bieżącą rzeczywistą wartość pola tekstowego, na przykład jeśli użytkownik wpisał tam coś po załadowaniu strony.
Ale:
$("#txt_name").attr('value')
Zwróci wartość z DOM / HTML.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-13 18:42:02
Możesz uzyskać atrybut value
bezpośrednio, ponieważ wiesz, że jest to element <input>
, ale twoje obecne użycie .val()
jest już obecny.
Dla powyższego, po prostu użyj .value
bezpośrednio na elemencie DOM, w następujący sposób :
$(document).ready(function(){
$("#txt_name").keyup(function(){
alert(this.value);
});
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-03 15:09:04
Myślę, że ta funkcja jest pominięta tutaj w poprzednich odpowiedziach
.val( function(index, value) )
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-22 06:21:34
Możesz uzyskać wartość w następujący sposób:
this['inputname'].value
Gdzie this
odnosi się do formularza, który zawiera dane wejściowe.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-04 16:40:09
Musisz użyć różnych sposobów, aby uzyskać bieżącą wartość elementu wejściowego.
Metoda - 1
Jeśli chcesz użyć prostej .val()
, spróbuj tego:
<input type="text" id="txt_name" />
pobieranie wartości z wejścia
// use to select with DOM element.
$("input").val();
// use the id to select the element.
$("#txt_name").val();
// use type="text" with input to select the element
$("input:text").val();
ustaw wartość na wejście
// use to add "text content" to the DOM element.
$("input").val("text content");
// use the id to add "text content" to the element.
$("#txt_name").val("text content");
// use type="text" with input to add "text content" to the element
$("input:text").val("text content");
Metoda - 2
Użyj .attr()
, aby uzyskać zawartość.
<input type="text" id="txt_name" value="" />
Dodaję tylko jeden atrybut do pola wejściowego. value=""
atrybutem jest ten, który nosi zawartość tekstowa, którą wprowadziliśmy w polu wprowadzania.
$("input").attr("value");
Metoda - 3
Możesz użyć tego bezpośrednio na swoim elemencie input
.
$("input").keyup(function(){
alert(this.value);
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-06 11:38:59
Aby uzyskać wartość pola tekstowego, możesz użyć funkcji jQuery val()
.
Na przykład,
$('input:textbox').val()
– Get textbox value.
$('input:textbox').val("new text message")
– Ustaw wartość pola tekstowego.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-04 14:39:28
Dla tych, którzy tak jak ja są nowicjuszami w JS i otrzymują undefined
zamiast wartości tekstowej Upewnij się, że twoje id
nie zawiera nieprawidłowych znaków.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-26 21:10:50