简体   繁体   English

如何在不使用提交的情况下从下拉列表中选择值

[英]How to select value from dropdown without using submit

I have two dropdowns: state and city.我有两个下拉菜单:州和城市。 Both are in the same form.两者都是相同的形式。 The goal is to somehow save the selection without clicking submit button so it could be used as a criteria to display selections of cities in the second dropdown.目标是以某种方式保存选择而不单击提交按钮,以便它可以用作在第二个下拉列表中显示城市选择的标准。 For example: When california is chosen in the first dropdown, second dropdwon displays all the cities in California.例如:当在第一个下拉列表中选择 california 时,第二个 dropdwon 将显示加利福尼亚的所有城市。

Code:代码:

<?php $db= DB::table('states_table')->get(); ?>
<select class="form-control input-md" name="state">
    <option value="" disabled selected>Choose the state</option>
    <?php foreach ($db as $data) { ?>
        <option value="<?php echo $data->city; ?>">
        <?php echo $data->city;?>
        </option><?php 
    }?>
</select>   

just use ajax :只需使用 ajax :

    $('#form').on('change','select[name="state"]', function() {
    var province = $('select[name=state]').val();
    $.ajax({
        url: './get_city.php',
        method: 'post',
        data: {"state": state},
        success: function (data) {
            $('select[name=city]').html(data);
        }
    })
});

and in the get_city.php connect to db , get the cities and return them on tags并在 get_city.php 连接到 db ,获取城市并在标签上返回它们

$('#state_field_id').click(function(){ var state=document.getElementById('state_field_name').options[document.getElementById('state_field_name').selectedIndex].text; });

你会得到选定的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用提交的情况下在php中提交下拉框值 - How to submit a dropdown box value in php without using submit 如何在不提交的情况下从文本框和下拉列表中获取值 - How to get the value from textbox & dropdown without submit 将值从第一个下拉列表提交到第二个下拉列表而不重新加载页面 - Submit value from first dropdown to second dropdown without reloading the page 如何在不使用php提交数据的情况下传递下拉列表中的选定值? - How to pass selected value in dropdown list without submit data using php? 如何从下拉列表中获取选定的值并在查询中使用它而不单击提交? - How to get the selected value from dropdown and use it in query without clicking submit? 如何从数据库获取文本框中的值取决于没有提交按钮的下拉列表 - How to get the value in Textbox from database depends on dropdown list without submit button 如何在不使用提交按钮的情况下从html表单传递值 - How to pass value from html form without using submit button 选择没有提交按钮的下拉菜单 - Select dropdown menu without submit button 如何从下拉列表中选择一个值,然后单击提交,然后在 div 部分的同一页面中打印值,进一步 div 也可以使用按钮? - how to select a value from dropdown then click on submit and then print value in same page in div section further div also working with button? 动态下拉列表选择在提交时不向db输入值 - dynamic dropdown select not entering value to db on submit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM