简体   繁体   English

为什么 HTML 表单变量似乎没有转移到 JS 函数?

[英]Why doesn't the HTML form variable seem to transfer to the JS function?

I've been working on this assignment for a while, and basically all it has to do is compare sorting algorithms and their number of comparisons.我已经在这个作业上工作了一段时间,基本上它所要做的就是比较排序算法和它们的比较次数。 It should ask for a number from the user and that will be the array size to sort.它应该要求用户提供一个数字,这将是要排序的数组大小。 However it seems the variable does not get passed and ends up being undefined as proven when I try and print it at "test2".然而,当我尝试在“test2”中打印它时,似乎该变量没有被传递并最终被证明是未定义的。 I tried having an OnClick to a function that assigns the element as you may see, but I abandoned that idea.我尝试将 OnClick 用于分配元素的函数,如您所见,但我放弃了这个想法。 And I have to admit I'm pretty new to this.我不得不承认我对此很陌生。 But any help would be appreciated!!但任何帮助将不胜感激! Thank you!!谢谢!!

<html>
  <title>Assignment 2</title>
  <body>
    <h2>Assignment 2</h2>
    <form id="form" method="GET" action="">
  Choose an amount of numbers to sort: <input type="number" name="sorter" id="choice" max="500" min="0"><br>
  <input type="submit" value="Submit">
</form>
<button type="button" id="InsertionSort" onclick="insertionSort()">Insertion Sort</button>
<button type="button" id="QuickSort" onclick="quickSort()">Quick Sort</button>
<button type="button" id="BubbleSort" onclick="bubbleSort()">Bubble Sort</button>
<button type="button" id="MergeSort" onclick="mergeSort()">Merge Sort</button>
<p id="test"></p>
<p id="test2"></p>
<p id="insertion"></p>
<p id="insertionCount"></p>

<script>
  //var input;
  //function assign(element)
  //{
  //  input = element.value;
  //  return input;
  //}
  function insertionSort()
{
  var input = document.getElementById("choice").value;
  var comparisons = 0;
  var arr = Array(input);
  document.getElementById("test2").innerHTML = input;
  for (var k = 0; k < arr.length; k++)
  {
    arr[k] = Math.random() * 1000;
  }...etc.
</html>

Welcome to the community.欢迎来到社区。 :) It's a challenging, but fun world. :) 这是一个充满挑战但又充满乐趣的世界。

I've got a working snippet of your code above.我有你上面代码的工作片段。 I'm not entirely sure what you're wanting to do from here, but the input is in scope of the function you've got.我不完全确定您想从这里做什么,但输入在您拥有的功能范围内。 Small syntactical errors prevented it from running properly initially, but you should be good now.小的语法错误阻止它最初正常运行,但现在你应该很好。

 <html> <title>Assignment 2</title> <body> <h2>Assignment 2</h2> <form id="form" method="GET" action=""> Choose an amount of numbers to sort: <input type="number" name="sorter" id="choice" max="500" min="0"><br> <input type="submit" value="Submit"> </form> <button type="button" id="InsertionSort" onclick="insertionSort()">Insertion Sort</button> <button type="button" id="QuickSort" onclick="quickSort()">Quick Sort</button> <button type="button" id="BubbleSort" onclick="bubbleSort()">Bubble Sort</button> <button type="button" id="MergeSort" onclick="mergeSort()">Merge Sort</button> <p id="test"></p> <p id="test2"></p> <p id="insertion"></p> <p id="insertionCount"></p> <script> function insertionSort() { var input = document.getElementById("choice").value; console.log(input); // prints out string version of number console.log(Number(input)) // prints out numeric version of number var comparisons = 0; var arr = Array(input); document.getElementById("test2").innerHTML = input; for (var k = 0; k < arr.length; k++) { arr[k] = Math.random() * 1000; } } </script> </body> </html>

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

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