首页 > 解决方案 > 如何从 HTML 选择中获取选定的文本以将其发送到数据库

问题描述

我有这个选择,值来自数据库

<select id="construido" name="construido" class="form-select form-select-sm" aria-label=".form-select-sm example">
                        <option id="construido-0" selected class="form-control"></option>
                        <option id="construido-1" value="1"> </option>
                        <option id="construido-2" value="2"> </option>
                    </select>

但现在我正在尝试使用 ajax php 将选定的文本发送到另一个表。我试过了,但我construidoSelected只能在函数内部访问

$(document).on('change', '#construido', function () {
    let construidoSelected = $("#construido option:selected").text()
});

let start = 0, url = "http://localhost:8077/connectpost/validations/sendinterview.php";
$(document).ready(function () {

    $('#form_interview').submit(function (e) {
        $.post(url, {
            fuente:"",
            construido:"",
            recibir: $("#recibir-agua").val(),
            horas: $("#horas-agua").val(),

        });
        $("#recibir").val("");
        $("#horas").val("");
        return false;
    })
});

标签: javascripthtmlajax

解决方案


推荐阅读