PHP中的自动数组

问题描述 投票:1回答:2

怎么样,举个例子,我们把初始数字和最终数字放在哪里。示例我们插入初始和最终编号:

Initial Number = 1 Final Number = 4

    Result = 1 2 3 4 

按SEND按钮时会抛出结果。

我想要的是我抛出结果而不必按下SEND按钮。执行FOR循环并在不按下按钮的情况下抛出结果。结果是自动的。

码:

<form action="" method="post">
  <input type="text" name="inivalue" id="inivalue" placeholder="initial value"/>
  <input type="text" name="finvalue" id="finvalue" placeholder="final value"/>
  <input type="submit" name="submit" vale="submit" />
</form>


<?php
if(isset($_POST['submit']))
{
$num = (int)$_POST['inivalue'];
$numfin = (int)$_POST['finvalue'];
for($num=$num; $num <= $numfin; $num++)
{
echo $num;
}
}
?>
javascript php html5
2个回答
0
投票

// Get your input elements using getElementById()
const initialValue = document.getElementById("inivalue");
const finalValue = document.getElementById("finvalue");
const result = document.getElementById("result");
let initialVal = "";
let finalVal = "";

// Every time you change the value in your <input> element
// save that value into the initialVal, finalVal variables.

initialValue.addEventListener("change", function(){
    initialVal = this.value;
    autoArray(initialVal,finalVal);
});

finalValue.addEventListener("change", function(){
    finalVal= this.value;
    autoArray(initialVal,finalVal);
});

// Loop using initialVal and finalVal
function autoArray(ini,fin){
  numArray = [];
  if (ini!= "" && fin != "") {
    for(i = ini; i <= fin; i++){
      numArray.push(i);
    }
  }
  // Change the value of the result <input> element
  result.value = numArray;
}
<input type="text" name="inivalue" id="inivalue" placeholder="initial value"/>
  <input type="text" name="finvalue" id="finvalue" placeholder="final value"/>
  <input type="text" id="result"/>

0
投票

可以这样做的一种方法是使用onChange event

将其设置在最终的数字字段中:

<input onchange = "rangefinder()" type="text" name="finvalue" id="finvalue" placeholder="final value"/>

然后在你的javascript函数rangefinder()

function rangefinder(){
    //get the value of both the invalue and finalvalue fields
    //make sure they're both integers - just return if they're not.
    //use a for loop to make a string of numbers from invalue to finalvalue
    //insert this string where ever you want it.
}

我会把实际的JS留给你。

© www.soinside.com 2019 - 2024. All rights reserved.