POST from jQuery Select

POST from jQuery Select

本文关键字:Select jQuery from POST      更新时间:2023-09-26

JsFiddle演示

<body>
<form method="POST" name="my">
<ol id="selectable">
  <li class="ui-widget-content" id="0">1</li>
  <li class="ui-widget-content" id="1">2</li>
  <li class="ui-widget-content" id="2">3</li>
  <li class="ui-widget-content" id="3">4</li>
  <li class="ui-widget-content" id="4">5</li>
</ol>
</form>
</body>

我想从后端获得响应中的选择菜单。我能够根据选择更改diplayed值,但不知道如何从所选输入中接收输入。请帮帮我!

您可以利用selected方法,该方法在回调中为您提供两个参数,即eventui对象。

ui对象包含selected对象,它是本机DOM元素。通过这种方式,我们可以包装它,然后对它使用jQuery方法(只是为了清晰和内聚)

从那里,我们可以构造对服务器的$.ajax()调用并执行我们的请求。一种可能的方法是:

selected: function (event, ui) {
    var $f = $('form[name="my"]'); //cache reference to the form wrapper
    $.ajax({
        url: $f.prop('action'),
        type: $f.prop('method'),
        data: {
            val: $(ui.selected).text() // this is the selected val.
        }
    }).done(function (data) {
        console.log(data); //return from server
    });
}

现在,在处理服务器请求的页面action="path/to/my/script.ext"中,您可以访问通过val选择的值,例如$_POST['val'];

Js Fiddle

单击菜单

时,您将获得所选文本