dpb_4431 2013-09-25 02:27
浏览 20
已采纳

jquery - 获取选择输入中所选选项的值

I'm quite a newbie when it comes to jquery. I'm using cakePHP and I have a form to register orders for a selected user, the select input for selecting the user looks like this in cakePHP:

echo $this->Form->input('user_id');

and that renders a form with an input like this:

<label for="OrderUserId">User</label>
<select name="data[Order][user_id]" id="OrderUserId">
    <option value="2"> User 2</option>
    <option value="3"> User 3</option>
</select>

I want to do an ajax call so that, when a user is selected, his info appears in a div. So far, I have this javascript:

$(document).ready(function(){
    $("#OrderUserId").change(function() {
        $.ajax({
            url: 'users/getData' //here would go the user ID
        });
    });
});

My question is this: how can I get the value of the selected option with jQuery so that I can pass it to the url in the ajax method?

  • 写回答

2条回答 默认 最新

  • ds2321 2013-09-25 02:29
    关注

    Just use plain js this.value. this inside your callback represents the dom element so you would just access its value using .value.

    $(document).ready(function(){
        $("#OrderUserId").change(function() {
             //var user = this.value;
            $.ajax({
                url: 'users/getData/' + this.value //here would go the user ID
            });
        });
    });
    

    Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥15 电脑出问题了,说是usbmom注册表没
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?