jQuery 按钮的 .click() 函数没有发生

jQuery button's .click() function isn't happening

本文关键字:函数 按钮 click jQuery      更新时间:2023-09-26

我有以下jQuery,我尝试使用.click()来计算文本输入中的符号。 单击该按钮不会执行任何操作。请帮忙,谢谢。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
 
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
  <input type="text"id="id1"></input></div><br/><br/>
	<button onclick="#"id="id2"> Count Symbols</button>
	<p id="id1"></p>
<script>
$( document.#id1 )
  .click(function() {
    $( document.#id1 ).append( $( "#id2" ) );
    var n = $( "#id2" ).length;
    $( ).text( "There are " + n + " symbols.");
  })
 
</script>
 
</body>
</html>

你可以

这样做:

$(document).ready(function(){
    $('#id2' ).click(function() {
    var len = $('#id1').val().length;
    $('#id3').text('There are ' +len+ ' symbols');
  });
}); //END document.ready

jsFiddle 演示

修订后的 HTML:

<input type="text" id="id1" />
<br/><br/>
<button id="id2"> Count Symbols</button>
<p id="id3"></p>