如何在不刷新页面或提交表单的情况下从下拉列表中检索选定值


How to retrieve selected value from the dropdown list without refreshing the page or form submitting?

我正在制作一个关于电子商务网站形式的品牌下拉菜单,我必须进入产品。在不使用提交按钮的情况下,是否有可能检索下拉菜单的选定值?更好的是,我可以使用没有封闭元素的select选项。如果这是可能的,我如何通过使用jquery检索下拉列表的值。

function getbrands_add_products(){
    global $con;
    try{
        $query = "select * from brands";
        $stmt = $con->prepare($query);
        $stmt->execute();
        $result = $stmt->fetchAll();
        echo "<option>Select Brand</option>";
        foreach ($result as $rows) {
            $brand_id = $rows['brand_id'];
            $brand_title = $rows['brand_title'];
            echo "<option value='$brand_id'>$brand_title</option>";
        }
    } catch(PDOException $e){
        echo "Error: ".$e->getMessage();
    }
}

这是我查询数据库并将其显示为用户选择的下拉列表的函数。我如何访问所选的值,而不使用表单提交。最好是jquery。

可以不使用<form>标签。

事实上,您可以使用任何表单相关的元素,而不需要<form>标签。

如果您想使用<select>元素的选择值,您可以使用select.onchange事件。

元素值是被选中的选项值。

html代码:

<select id='select'>
    <option value='11'>
        testOption
    </option>
</select>

js代码:

var select = document.getElementById('select');
select.onchange = function () {
    var value = this.value;
    alert(value);
}

你可以在表单外使用<select>,但这不是标准的方式。

可以使用javascript jquery访问该值。给它一个像

这样的id
<select id="select-box">
<option value = "something"></option>
....

然后在jquery上使用:

$("#select-box").on("change",function(){
    var value = $(this).val()
})

但是在

之外使用选择框不是标准的方式