Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在数组中对列表进行排序,并在文本框中以排序方式显示_Javascript_Php - Fatal编程技术网

Javascript 如何在数组中对列表进行排序,并在文本框中以排序方式显示

Javascript 如何在数组中对列表进行排序,并在文本框中以排序方式显示,javascript,php,Javascript,Php,这是index.php 此Javascript用于随机数,并显示在9个文本框中 <script> function GetRandom() { var myElement = document.getElementById("no1") myElement.value = Math.floor((Math.random() * 1000) + 1); var myElement

这是index.php

此Javascript用于随机数,并显示在9个文本框中

  <script>
        function GetRandom()
        {
            var myElement = document.getElementById("no1")
            myElement.value = Math.floor((Math.random() * 1000) + 1);
            var myElement = document.getElementById("no2")
            myElement.value = Math.floor((Math.random() * 100) + 1);
            var myElement = document.getElementById("no3")
            myElement.value = Math.floor((Math.random() * 10000) + 1);
            var myElement = document.getElementById("no4")
            myElement.value = Math.floor((Math.random() * 1000) + 1);
            var myElement = document.getElementById("no5")
            myElement.value = Math.floor((Math.random() * 10000) + 1);
            var myElement = document.getElementById("no6")
            myElement.value = Math.floor((Math.random() * 1000) + 1);
            var myElement = document.getElementById("no7")
            myElement.value = Math.floor((Math.random() * 1000) + 1);
            var myElement = document.getElementById("no8")
            myElement.value = Math.floor((Math.random() * 1000) + 1);
            var myElement = document.getElementById("no9")
            myElement.value = Math.floor((Math.random() * 10000) + 1);
        }
    </script>

函数GetRandom()
{
var myElement=document.getElementById(“no1”)
myElement.value=Math.floor((Math.random()*1000)+1);
var myElement=document.getElementById(“no2”)
myElement.value=Math.floor((Math.random()*100)+1);
var myElement=document.getElementById(“no3”)
myElement.value=Math.floor((Math.random()*10000)+1);
var myElement=document.getElementById(“no4”)
myElement.value=Math.floor((Math.random()*1000)+1);
var myElement=document.getElementById(“no5”)
myElement.value=Math.floor((Math.random()*10000)+1);
var myElement=document.getElementById(“no6”)
myElement.value=Math.floor((Math.random()*1000)+1);
var myElement=document.getElementById(“no7”)
myElement.value=Math.floor((Math.random()*1000)+1);
var myElement=document.getElementById(“no8”)
myElement.value=Math.floor((Math.random()*1000)+1);
var myElement=document.getElementById(“no9”)
myElement.value=Math.floor((Math.random()*10000)+1);
}
这个javascript将9个文本框的值放入数组和排序列表中

    <script>
        function sortlist()
        {
            var captureNumb = document.getElementById("no1");
            var captureNumb = document.getElementById("no2");
            var captureNumb = document.getElementById("no3");
            var captureNumb = document.getElementById("no4");
            var captureNumb = document.getElementById("no5");
            var captureNumb = document.getElementById("no6");
            var captureNumb = document.getElementById("no7");
            var captureNumb = document.getElementById("no8");
            var captureNumb = document.getElementById("no9");
            var captureNumb = document.getElementById("no1");

            var numbers = captureNumb.value.split(" ");
            captureNumb.value = numbers.sort(function (a, b) {
                return a - b
            }).join(" ");
        }
    </script>

</head>
<body>
    <div class="form-inline">
        <div class="col-xs-12">
            <div class="form-group">

                <input type="text" class="form-control" id="no1" name="no1" placeholder="No 1">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no2" name="no2" placeholder="No 2">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no3" name="no3" placeholder="No 3">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no4" name="no4" placeholder="No 4">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no5" name="no5" placeholder="No 5">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no6" name="no6" placeholder="No 6">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no7" name="no7" placeholder="No 7">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no8" name="no8" placeholder="No 8">
            </div>
            <div class="form-group">
                <label for="exampleInputName2"></label>
                <input type="text" class="form-control" id="no9" name="no9" placeholder="No 9">
            </div>
            <br><br><br> 
            <div align="center">



                <button type="button" class="btn btn-primary" onclick="GetRandom()">Populate</button>

                <button type="button" class="btn btn-primary" onclick="sortlist()" >Sort list</button>
                <button type="button" class="btn btn-primary">Primary</button>
            </div>
        </div>
    </div>

函数sortlist()
{
var capturenamb=document.getElementById(“no1”);
var capturenamb=document.getElementById(“no2”);
var capturenamb=document.getElementById(“no3”);
var capturenamb=document.getElementById(“no4”);
var capturenamb=document.getElementById(“no5”);
var capturenamb=document.getElementById(“no6”);
var capturenamb=document.getElementById(“no7”);
var capturenamb=document.getElementById(“no8”);
var capturenamb=document.getElementById(“no9”);
var capturenamb=document.getElementById(“no1”);
变量编号=captureNumb.value.split(“”);
captureNumb.value=numbers.sort(函数(a,b){
返回a-b
}).加入(“”);
}



居住于 排序表 主要的,重要的
  • 如何单击按钮排序列表并重新显示排序列表
  • 试图从9个文本框中捕获值
    您的代码需要认真研究。有许多重复的语句可以用循环来解决,在每一行重新定义变量的地方,比如
    var myElement=…
    一行中的两行,将其分配给变量是没有意义的,因为您从不引用
    myElement
    ,它的值在下一行被覆盖

    就排序方面的请求而言,您需要从某些数据结构中动态插入它们

    <?php
        $values = someGeneratingFunction();
        $values = someSortFunction($values);
        for($i=0;$i<count($values);$i++) {
            echo "<input type=\"text\" class=\"form-control\" id=\"no" . $i . "\" name=\"no" . $i . "\" placeholder=\"No 1\">"
        }
        // Note the insertion of the ID values ("no1", "no2" etc.) using the loop instead of repeatedly typing the same thing.
    ?>
    

    我们初始化一个数组,以保存由id
    “no1”
    “no2”
    等标识的所有元素。然后我们在一个for循环中逐个推进它们,该循环访问它们,所有这些元素都使用索引(注意,这就是我得到的,因为这比在一行中重复相同的代码10行,只更改一个数字要好),然后返回排序后的数组。请注意,当调用
    values.sort(函数(a,b){returna-b})
    时,我们将函数作为参数传递给。这是因为默认情况下,
    .sort()
    方法按字母数字升序将字符串排序。因此数组
    [1,3,200,15000]
    将被排序为
    [1,15000,200,3]
    ,这显然不是数字顺序。通过提供附加函数作为参数,我们告诉它减去两个(它充当比较器),然后将为我们提供准确的数值结果。

    您的代码需要认真研究。有许多重复的语句可以用循环来解决,在每一行重新定义变量的地方,比如
    var myElement=…
    一行中的两行,将其分配给变量是没有意义的,因为您从不引用
    myElement
    ,它的值在下一行被覆盖

    就排序方面的请求而言,您需要从某些数据结构中动态插入它们

    <?php
        $values = someGeneratingFunction();
        $values = someSortFunction($values);
        for($i=0;$i<count($values);$i++) {
            echo "<input type=\"text\" class=\"form-control\" id=\"no" . $i . "\" name=\"no" . $i . "\" placeholder=\"No 1\">"
        }
        // Note the insertion of the ID values ("no1", "no2" etc.) using the loop instead of repeatedly typing the same thing.
    ?>
    
    我们初始化一个数组,以保存由id
    “no1”
    “no2”
    等标识的所有元素。然后我们在一个for循环中逐个推进它们,该循环访问它们,所有这些元素都使用索引(注意,这就是我得到的,因为这比在一行中重复相同的代码10行,只更改一个数字要好),然后返回排序后的数组。请注意,当调用
    values.sort(函数(a,b){returna-b})
    时,我们将函数作为参数传递给。这是因为默认情况下,
    .sort()
    方法按字母数字升序将字符串排序。因此数组
    [1,3,200,15000]
    将被排序为
    [1,15000,200,3]
    ,这显然不是数字顺序。通过提供附加函数作为参数,我们告诉它减去两个(它充当我们的比较器),然后将为我们提供准确的数值结果