繁体   English   中英

Javascript 数组在按钮点击后实时变化

[英]Javascript array change in realtime after button click

我正在制作一个 web 应用程序,它可以帮助人们根据症状寻找他们患有的疾病。

我希望用户单击特定症状以添加到“u_symptom_i”数组中,并通过警报 function 显示所有更改的数组元素

但是,我看不到警报 function 添加的元素

    <script>
      var j = 0;
      while(j < escaped_cc.length) {
        document.write('<th><button id="symptom_button">' + escaped_cc[j] + '</button></th>');
        document.getElementById("symptom_button").value = escaped_cc[j];
        j = j + 1;
      }

      $("button").click(function() {
          u_symptom_i.push($(this).val());
          alert($(this).val());
      });
    </script>
<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<body>
<h2>Insert Array</h2>
<input type="text" id="example" name="value">
<button id="button">Add array new item</button>
</body>
<script>
var array=[];
$("#button").click(function() {
  var str = $("#example").val();
  array.push(str);
  alert(array);
});
</script>
</html> 

你可以试试这个代码吗? 将输入的每个新值添加到名为 array 的数组并显示记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM