[英]Can't show text from an input when click button with jQuery
我正在嘗試編寫一個簡單的表單,該表單將獲取一些文本,然后在我單擊“提交”按鈕時顯示在對話框中。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Página Teste</title>
</head>
<body>
<header>
<div>
<h1>Questão 4</h1>
</div>
</header>
<div>
<label for="texto">Texto: </label>
<textarea name="texto" id="texto" rows="5" cols="10"></textarea>
<button type="submit" id="submit">Enviar</button>
</div>
<script>
var teste = $("texto").val;
$("submit").on("click", function(){
$("teste").dialog("open");
});
</script>
</body>
</html>
當我單擊該按鈕時,什么都沒有發生,並且我嘗試了一些執行此腳本的方法,但似乎沒有任何效果。
您缺少一些#
而.val
是一個函數,因此請使用.val()
。
我不確定我是否理解您要使用的對話框,但是應該嘗試如下操作:
$("#submit").on("click", function(){
var teste = $('#texto').val();
alert(teste);
});
嘗試這個:
$(document).ready(function(){
var teste = $("texto").val;
$("#submit").on("click", function(){
console.log("something happens");
});})
您可以使用准備好的文檔。 另外,如果您願意,可以有單獨的javascript文件並執行更多操作
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Página Teste</title>
</head>
<body>
<header>
<div>
<h1>Questão 4</h1>
</div>
</header>
<div>
<label for="texto">Texto: </label>
<textarea name="texto" id="texto" rows="5" cols="10"></textarea>
<button type="submit" id="submit">Enviar</button>
</div>
<script>
$(document).ready(function(){
var teste = $('#texto').val();
$("#submit").on("click", function(){
alert("teste")
});})
</script>
</body>
</html>
當您調用$("teste").dialog("open");
或$("texto").val()
jQuery將在dom中查找元素<teste></teste>
。 由於該元素在您的示例中不存在,因此不會執行任何操作。
也許您錯過了.
或#
傳遞給$(selector)
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.