简体   繁体   English

选择选项值使用ajax发送到php文件

[英]select option value send to php file with ajax

I have a select dropdown box 我有一个选择下拉框

<select name="status" id="status" onchange="changeStatus()">
    <option value="step_1">step_1</option>
    <option value="step_2">step_2</option>
    <option value="step_3">step_3</option>
    <option value="step_4">step_4</option>
</select>

And my javascript 还有我的javascript

<script>
    function changeStatus() {
        $('select.changeStatus').change(function(){
            $.ajax({
                    type: 'POST',
                    url: 'update_status.php',
                    data: {changeStatus: $('select.changeStatus').val()},
                    dataType: 'html'
             });
        });
    });
</script>

So I want the value selected from the select dropdown box send to the php file(update_status.php) 所以我想从选择下拉框中选择的值发送到php文件(update_status.php)

You don't need to mix jQuery and inline JS 您无需混合使用jQuery和内联JS

HTML - we'll use your selects name in the function. HTML-我们将在函数中使用您的selects名称。 I've removed the inline function call: 我删除了内联函数调用:

<select name="status" id="status">
    <option value="step_1">step_1</option>
    <option value="step_2">step_2</option>
    <option value="step_3">step_3</option>
    <option value="step_4">step_4</option>
</select>

jQuery - I have taken the code out of the function you were calling inline, it was not needed. jQuery-我已将代码从您内联调用的函数中删除,这不是必需的。

<script>
    $(document).ready(function() {
        $('select[name="status"]').change(function(){
            var status = $(this).val();
            $.ajax({
                    type: 'POST',
                    url: 'update_status.php',
                    data: {changeStatus: status},
                    dataType: 'html'
             });
        });
    });
</script>

You can directly test when the select changes and grab its value right then. 您可以直接测试select何时更改,然后立即获取其值。 Here is an EXAMPLE 这是一个例子

I have also wrapped the jQuery in a document ready handler. 我也将jQuery包装在文档就绪处理程序中。 Depending on where you're loading the script in your page you may need to do this to make sure that your jQuery is run as soon as the DOM elements have been loaded. 根据您要在页面中加载脚本的位置,可能需要执行此操作以确保在加载DOM元素后立即运行jQuery。

$('#status').val() is used to get value of select options. $('#status')。val()用于获取选择选项的值。

So try this 所以试试这个

<script>
    function changeStatus() {
        $.ajax({
                    type: 'POST',
                    url: 'update_status.php',
                    data: {changeStatus: $('#status').val()},
                    dataType: 'html'
             });
    });
</script>

or 要么

<script>

function changeStatus() {
    $.post("update_status.php", {changeStatus: $('#status').val()}, function(data, status){

    });
}

</script>

Select id using # . 使用#选择ID。 Since you are already using javascript onchange event in select field so no need to use jquery change() event. 由于您已经在选择字段中使用了JavaScript onchange事件,因此无需使用jquery change()事件。 jQuery use # to select id and . jQuery使用#选择ID和. to select class. 选择班级。 since you are using id="status" so you must use # to select the id. 由于您使用的是id="status"因此必须使用#来选择ID。 The way you are trying to collect the value of the select field in this line $('select.changeStatus').val() is wrong. 您试图在此行$('select.changeStatus').val()收集选择字段值的方式是错误的。 Because there are no class named changeStatus there. 因为那里没有名为changeStatus类。

function changeStatus() {

        $.ajax({
                type: 'POST',
                url: 'update_status.php',
                data: {changeStatus: $('#select').val()},
                dataType: 'html'

        });
}

Remove the onchange field from select and put the below code to your script and this is working. 从select中删除onchange字段,然后将以下代码放入您的脚本中,这可以正常工作。 Use JQuery post functionality instead. 请改用JQuery发布功能。

$("#status").change( function() {

    alert($( this ).val());
    $.post('update_status.php', { changeStatus: $(this).val()},      
         function(response) {
         console.log( response);

    });

});
       <select name="status" id="status" onchange="changeStatus(this.value)">
            <option value="step_1">step_1</option>
            <option value="step_2">step_2</option>
            <option value="step_3">step_3</option>
            <option value="step_4">step_4</option>
        </select>


function changeStatus(value) {

        $.ajax({
                type: 'post',
                url: 'update_status.php',
                data: {'get_data:value'},
                success: function (response) {
                       document.getElementById("loadto").innerHTML=response; 
                     }

        });
}

I Hope it will be helpful ..... 希望对您有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM