简体   繁体   English

使用HTML下拉菜单分配JavaScript变量(用户选择)

[英]Assigning JavaScript variables with HTML drop-down menu (user selection)

I am brand new to JavaScript so please bear with me! 我是JavaScript的新手,所以请耐心等待! I'm building a slider puzzle, and I'm trying to create a form that will allow the user to pick the dimensions of their puzzle. 我正在构建一个滑块拼图,我正在尝试创建一个允许用户选择拼图尺寸的表单。 In the past, I've had the variables for row (_r) and column (_c) set to 3 - but what I'd like now is an HTML select table where the user can pick values between 3 and 5 for each and then generate their own table. 在过去,我已经将行(_r)和列(_c)的变量设置为3 - 但我现在想要的是一个HTML选择表,用户可以在其中为每个选择3到5之间的值,然后生成自己的表。

I'm just not sure how to properly assign _r and _c based on the user's selection, and everything I've searched related to different HTML forms or wasn't otherwise helpful. 我只是不确定如何根据用户的选择正确分配_r和_c,我搜索的所有内容都与不同的HTML表单相关或者没有其他帮助。

Also, if anyone knows a fix for this: Before I created the form all I had was a "New Game" button (and like I said, _r and _c were both set to 3), when I clicked the New Game button everything worked perfectly, but if I click it again it keeps generating empty tables beneath the original one which refreshes normally. 此外,如果有人知道这个问题的解决方法:在我创建表单之前,我所拥有的只是一个“新游戏”按钮(就像我说的那样,_r和_c都设置为3),当我点击New Game按钮时,一切正常完美,但如果我再次点击它,它会在原始表格下方生成空表,这些表格会正常刷新。 Any ideas how to prevent that? 任何想法如何预防? (this is why I have the clearPuzzle function at the end) (这就是我最后有clearPuzzle功能的原因)

Thank you for any insight. 感谢您的任何见解。

Here is my HTML: 这是我的HTML:

    <p>Choose Dimensions: </p>
    <select name="Rows" onchange="form()">
        <option value="Rows" selected disabled>Rows</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>
    <select name="Columns" onchange="form()">
        <option value="Columns" selected disabled>Columns</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>


<input type="button" name="button" value="New Game" onclick="init(_r, _c)"></button>

and JavaScript: 和JavaScript:

/*what I had before with no form function: 
_r = 3;
_c = 3;
*/

 function form()
    {
      var _r = document.getElementsByName("Rows")[0].value;
      var _c = document.getElementsByName("Columns")[0].value;
    }

function init(r, c)
{
   form();
   var puzzle = new puzzleArray(r, c);
   shuffle(puzzle);
   displayPuzzle(puzzle);
   addClickHandlers();
}

function puzzleArray(r, c)
{
   //declare and populate array
   var _array = [];

   for (var i = 0; i < r*c; i++)
   {
      _array[i] = i;
   }
   return _array;
}

function shuffle(_array) 
{
 //shuffle tiles
 for (var i = 0; i < _r*_c; i++)
 {
   var rand = Math.floor(Math.random() * _array.length);
   var temp = _array[rand];
   _array[rand] = _array[i];
   _array[i] = temp;
 }

 //check to see if puzzle is solveable
 var count = 0;
 do {
 for (var i = 0; i < _r*_c; i++)
 {
   for (var j = i; j <= _r*_c; j++)
   {
      if (_array[j] < _array[i])
      {
         count++;
      }
   }
 }
 } while (Math.floor(count/2) != count/2);

}


function displayPuzzle(anArray)
{
   //create table
   var table = document.createElement("table");
   table.id = "myTable";

   for (var i = 0; i < _r; i++)
   {
      var row = document.createElement('tr');
      for (var j = 0; j < _c; j++)
      {
         var column = document.createElement('td');
         row.appendChild(column);    
      }
      table.appendChild(row);
   }
   document.body.appendChild(table);

   //populate cells with their original and shuffled values.
   for (var i = 0; i < _r*_c; i++)
   {
      document.getElementsByTagName("td")[i].innerHTML = "Cell: " + [i] + ", " + "Value: " + anArray[i];

         if (anArray[i] == 0)
         {
            document.getElementsByTagName("td")[i].innerHTML = "Cell: " + [i] + ", " + "Value: " + "Blank!";
         }
   }

   //specify classes for each cell
   var _cell = document.getElementsByTagName("td");
    for (var i = 0; i < _r*_c; i++)
   {
      _cell[i].id = "s" + [i];
   }

}

function addClickHandlers()
{
   for (var i = 0; i < _r*_c; i++)
   {
      var cells = document.getElementsByTagName("td");   
      cells[i].onclick = moveTile;
   }
}

function moveTile()
{

      this.innerHTML += "!!!"; //just experimenting
}

//not working yet
function clearPuzzle()
{
   var puzzle = new puzzleArray(r, c);
   shuffle(puzzle);
}

I'm concerned that the function form has local variables declarations for variables _r and _c. 我担心函数form有变量_r和_c的局部变量声明。 As local variables, they will be discarded on function exit, whereas I think you are trying to assign to (document) global variables. 作为局部变量,它们将在函数退出时被丢弃,而我认为您正在尝试分配给(文档)全局变量。 You should omit the var keyword there. 你应该在那里省略var关键字。 Document global variables should be declared outside of any function scope. 文档全局变量应在任何函数范围之外声明。


/*what I had before with no form function: 
_r = 3;
_c = 3;
*/
var _r;    // declare _r & _c as document global variables
var _c;

function form()
{
  // assign to the document global variables.
  _r = document.getElementsByName("Rows")[0].value;
  _c = document.getElementsByName("Columns")[0].value;
}

Figured it out: form() was en element being used elsewhere, so I changed the function name to sndForm() and also edited the code within that function: 弄清楚:form()是在其他地方使用的元素,所以我将函数名称更改为sndForm()并编辑了该函数中的代码:

HTML: HTML:

<select id = "_rows" onchange="sndForm()">
    <option value="Rows" selected disabled>Rows</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>
<select id="_columns" onchange="sndForm()">
    <option value="Columns" selected disabled>Columns</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

JavaScript: JavaScript的:

var _r;
var _c;
var row;
var col;

function init(_r, _c)
{
   //alert(_r);
   //alert(_c);
   sndForm();
   var puzzle = new puzzleArray(_r, _c);
   shuffle(puzzle);
   displayPuzzle(puzzle);
   //alert(puzzle);
   startTimer();
   addClickHandlers();
}

function sndForm()
{
   row = document.getElementById("_rows");
   col = document.getElementById("_columns");
   _r = row.options[row.selectedIndex].value;
   _c = col.options[col.selectedIndex].value;
}

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

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