【问题标题】:How to get values from two or more select in jQuery (ajax)如何从 jquery (ajax) 中的两个或多个选择中获取值
【发布时间】:2022-11-18 01:14:51
【问题描述】:

这是 HTML 中的选择和选项。我在这里使用 jquery。我需要获取两个 select 的值并将值传递给 ajax.php,我需要在其中使用这两个值来检查我的数据库并继续进行另一个相关的下拉列表。

<label>Select Number: </label>
<select id= "number" onchange="FetchState(this.value)" required>
      <option value="0">-- 0 --</option>
      <option value="1">-- 1 --</option>
      <option value="2">-- 2 --</option>
      <option value="3">-- 3 --</option>
</select>

<label>Select letter: </label>
<select id="alphabet" required>
      <option value="a">-- a --</option>
      <option value="b">-- b --</option>
      <option value="c">-- c --</option>
      <option value="d">-- d --</option>
</select>
<select id="dependent">
</select>

这是我下面写的脚本代码。

function FetchState(id) {
        $("#dependent").html('');
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: {
                number: id,
            },
            success: function(data) {
                $("#dependent").html(data);
            }
        });
    }
</script>

我需要通过 jquery(ajax) 将两个值传递给 PHP 才能继续。这里字母和字母值都应该被获取并作为 jquery 中的数据传递给 ajax.php

【问题讨论】:

  • 因此,您知道另一个下拉列表的 id,首先检查某些内容是否已选中,然后获取所选项目的 .val。如果您只是启动一两个 DuckDuck 搜索,那么实际上必须有 1000 个示例
  • 获取值很好,但是如何在 jquery 中一次将两个值实际传递给 ajax.php
  • data: { number: id,alpha: alp },

标签: javascript php jquery ajax


【解决方案1】:

声明一个数组,推送到它,然后在到达数组中的两个项目时触发 ajax 调用怎么样?

var bothnumbers = [];

function FetchState(id) {
bothnumbers.push(id);
console.log('your array: '+bothnumbers);

if(bothnumbers.length == 2){
    console.log('fire function now that two are chosen');
        $("#dependent").html('');
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: {
                number: bothnumbers,
            },
            success: function(data) {
                $("#dependent").html(data);
            }
        });
}

    }

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2015-01-09
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    相关资源
    最近更新 更多