简体   繁体   English

如何从Jquery UI Selectable获取值

[英]How to get values from Jquery UI Selectable

I have a question regarding the Jquery US Selectable I have already looked at this post and I still cant figure out the problem: How to get the value from jQuery UI selectable in php I would like to get all the values I selected from the list (not the indexes but the values!!) into a array and than send it to PHP (via POST method). 我有一个关于Jquery US Selectable的问题,我已经看过这篇文章了,但是我仍然无法弄清楚问题: 如何从php的jQuery UI中获取值我想从列表中获取所有我选择的值(而不是索引,而是将值!)放入数组中,然后将其发送到PHP(通过POST方法)。 I have prepared a fiddle here 在这里准备了一个小提琴

I have to select the values and not the indexes. 我必须选择值而不是索引。 Example of the list: 列表示例:

<ol id="selectable">
  <li class="ui-widget-content" value='1'>Item 1</li>
  <li class="ui-widget-content" value='2'>Item 2</li>
  <li class="ui-widget-content" value='3'>Item 3</li>
  <li class="ui-widget-content" value='4'>Item 4</li>
  <li class="ui-widget-content" value='5'>Item 5</li>
  <li class="ui-widget-content" value='6'>Item 6</li>
  <li class="ui-widget-content" value='7'>Item 7</li>
</ol>

From this list I have to select the values. 我必须从该列表中选择值。 The code below only gets the index of the list. 下面的代码仅获取列表的索引。 I would like to know how to select the values and send the values to the same file ('index.php'). 我想知道如何选择值并将这些值发送到同一文件('index.php')。 Than if the array is set I can afterwards manipulate with the data. 比起设置数组,我可以在以后处理数据。

$( function() {
$( "#selectable" ).selectable({
  stop: function() {
    var result = $( "#select-result" ).empty();
    $( ".ui-selected", this ).each(function() {
      var index = $( "#selectable li" ).index( this );
      result.append( " #" + ( index + 1 ) );
    });
  }
 });
});

Thank you for your help in advance. 提前谢谢你的帮助。 I'd really like to know how to get the values to an array and send it to the php file. 我真的很想知道如何将值获取到数组并将其发送到php文件。

You can get the element reference from this inside the stop function. 你可以从元素参考this停止功能里面。 Based on that, you can access the value attribute using .attr() . 基于此,您可以使用.attr()访问value属性。 Example: 例:

Code: 码:

$(function() {
  $("#selectable").selectable({
    stop: function() {
      var result = $("#select-result").empty();
      $(".ui-selected", this).each(function() {
        result.append(" #" + ($(this).attr("value")));//$(this).attr("value")
      });
    }
  });
});

Demo: http://jsfiddle.net/GCu2D/2068/ 演示: http//jsfiddle.net/GCu2D/2068/

You can use the result's value in whatever way you want. 您可以使用任何方式使用结果的值。 You can use ajax to pass these to the PHP backend 您可以使用ajax将其传递给PHP后端

$(".ui-selected", this).each(function () {

    // this works for me
    result.append("<li>" + $(this).text() + "</li>");
});

I hope that works out :-) 我希望能解决:-)

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

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