如何设置子元素的最小和最大数据

时间:2019-05-11 22:53:49

标签: javascript php jquery html

你好,我无法从数据库中设置最小值和最大值。

实际上,我想通过PHP使其动态。在后端,应该有两个字段,第一个是最小值,第二个是最大值。在最大字段中,我可以设置最大值,在最小字段中,可以设置,然后在我的编码中通过PHP调用它。

我们可以做点什么吗?这是编码。

<html>
<body>
<head>
</head>

<?php

  echo '
    <script>
    $(document).on("change", ".qty1", function() {
        var sum = 0;
        $(".qty1").each(function(){
            sum += +$(this).val();
        });
        $(".total").val(sum);
    });

    </script>';

$maxnum = 7;
if(num > $maxnum){
echo "<script>alert('Yes it is big');</script>";
}


?>
    <input type="text" class="qty1" value="" />
        <input type="text" class="qty1" value="" />
        <input type="text" class="qty1" value="" />
        <input type="text" class="qty1" value="" />
        <input type="text" class="qty1" value="" />
        <input type="text" class="qty1" value="" />
        <input type="text" class="total" value="" />
        </body></html>

我该如何在jQuery中使用php或更改编码。我不知道如何更改编码,请问有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以将所有值放入数组中,然后获取最小值/最大值

#include <iostream>
#include <string>
#include <string.h>
using namespace std;

int main() {
    const int SIZE = 20;
    string temp;
    int i, j;
    string arr[SIZE] = { "Collins, Bill", "Smith, Bart", "Michalski, Joe",
        "Griffin, Jim","Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone",
        "Johnson, Jill","Allison, Jeff", "Moreno, Juan", "Wolfe, Bill",
        "Whitman, Jean","Moretti, Bella", "Wu, Hong", "Patel, Renee",
        "Harrison, Rose","Smith, Cathy", "Conroy, Pat", "Kelly, Sean", "Holland, Beth" };
    for (i = 0; i < (SIZE - 1); i++)
    {
        int k = i;
        for (j = (i+1); j < SIZE; j++)
        {
            if ( arr[k] > arr[j])
            {
               k = j;
            }
        }
        if (i != k) {
            temp = arr[i];
            arr[i] = arr[k];
            arr[k] = temp;
        }
    }
    for (int j = 0; j < SIZE; j++) {
        cout << arr[j] << endl;
    }
    system("pause");
    return 0;
}

工作小提琴https://jsfiddle.net/w47fLjcu/