如何根据其他组合框的选定值填充组合框?

时间:2019-08-10 00:07:40

标签: javascript php html ajax

我要做的就是发送listlist中选择的选项的值,并用slct1填充cb_insumo_update.php,而无需刷新页面或使用表单按钮。

我尝试过ajax,但是我是新手,所以我不知道如何使它工作。

问题是我正在使用数据库填充选择标签,并且在使用过程中有些困惑:

slct1

cb_categoria_update

cb_insumo_update.php

cb_insumo_update

<section class="container">

    <div class="row" >

      <div class="input-field  col s12" >
        <select id="slct1" name="slct1" onchange="populate('slct1','slct2')">
          <?php  require('php/cb_categoria_update.php');  ?>
        </select>
        <label>Categoría</label>
      </div>


      <div class="input-field col s12" method="GET">
        <select id="slct2" name="slct2">
         </select>  
        <label>Insumo</label>
      </div>

    </div>
</section>

1 个答案:

答案 0 :(得分:0)

我将以jQuery为例,它比普通的JavaScript更容易组合在一起,因此您需要在标题(或页面底部)中使用jQuery库:

static Scanner scanner = new Scanner(System.in);

public static void main(String[] args)
{
    int[] money = new int[4];
    money = initialCash();

}
public static int[] initialCash()
{
    int[] initialMoney = new int[4];

    while(true)
    {
        System.out.print("Ones: ");
        initialMoney[0] = scanner.nextInt();
        System.out.print("Fives: ");
        initialMoney[1] = scanner.nextInt();
        System.out.print("Tens: ");
        initialMoney[2] = scanner.nextInt();
        System.out.print("Twenties: ");
        initialMoney[3] = scanner.nextInt();

        if((initialMoney[0]>=0)&&(initialMoney[1]>=0)&&(initialMoney[2]>=0)&&(initialMoney[3]>0))
        {
            return initialMoney;
        }
        else
        {
            System.out.println("One or more invalid denominations. Try again.");
        }
    }

}

重要说明,您将希望在此查询上绑定参数:

<section class="container">
    <div class="row" >
      <div class="input-field col s12" >
        <select id="slct1" name="slct1">
          <!-- You can keep this as is -->
          <?php require('php/cb_categoria_update.php');  ?>
        </select>
        <label>Categoría</label>
      </div>
      <div class="input-field col s12" method="GET">
        <!-- Use a jQuery event listener instead of the inline onchange -->
        <select id="slct2" name="slct2">
         </select>  
        <label>Insumo</label>
      </div>
    </div>
</section>

<script>
// Document ready
$(function(){
    // Listen for change on first drop down
    $('#slct1').on('change', function(){
        // Run ajax
        $.ajax({
            // Set the url for the file you want to load into current page
            'url': 'cb_insumo_update.php',
            // You could use GET here, I just use post
            'type': 'post',
            // Fetch the value of the current selected option
            'data': $(this).val(),
            // Take the html result from the php page
            'success': function(response) {
                // Place it into this page
                $('#slct2').html(response);
            }
        });
    });
});
</script>

这不是运行SQL查询的安全方法。

或者,您可以对ajax found on this page执行“简写”方法。