在onChange事件上将Form元素值传递给Ajax Post方法

Pass Form Element Value to Ajax Post Method on an onChange Event

本文关键字:Ajax Post 方法 值传 元素 事件 onChange Form      更新时间:2023-09-26

所以我有一个this表单选择,我想使用onChange事件在ajax中传递值。

<select name="someName" id="someName" onChange="changeSomething()">
    <option value="1">Value 1</option>
    <option value="2">Value 2</option>
    <option value="3">Value 3</option>
    <option value="4">Value 4</option>
</select>
    <div id="base_url"></div>
    <script>
        function changeSomething() {       
            $.ajax({
                url: "http://example.com/ajax/command/change_something.php",
                type: "POST",
                async: true,
                cache: false,
                data: {
                    "key": "2qsKeJrQNLE1GoFrnWpL",
                    "base_url": "http://example.com/",
                    "id": $("#someName").val() //  I want to get the value of this one
                },
                success: function (data) {
                    $("#base_url").html(data);
                }
            });
        }
    </script>

有没有其他方法可以传递任何表单元素的值,尤其是在onChange事件上?我做错了吗?请帮忙!!T_T

您可以使用$.serializeArray()JQuery函数序列化表单元素

请参阅上的文档http://api.jquery.com/serializeArray/

检查这个:

   <select name="someName" id="someName" onChange="changeSomething(this.value)">
   function changeSomething(id)
   "id": id