开发者

HTML Multi-select to Ajax URL

开发者 https://www.devze.com 2022-12-12 20:26 出处:网络
Let\'s assume we have an HTML multi-select. How to prepare an ajax request URL with the selected values from the multi-select. I\'m using YUI dataTable, so I do not think I can use form serialization

Let's assume we have an HTML multi-select.

How to prepare an ajax request URL with the selected values from the multi-select. I'm using YUI dataTable, so I do not think I can use form serialization (but would not even want to even if I could)? The URL gener开发者_JAVA技巧ated should be the same as with a normal form submission with page refresh.

I did this but was hoping a better way with YUI was possible:

function createURLFromMultiSelect(ob, name) { 
  var url = "";
  for (var i = 0; i < ob.options.length; i++) {
      if (ob.options[ i ].selected) {
          url += "&" + name + "[]=" + ob.options[ i ].value;
    }  
  }
  return url;
} 


name=value1&name=value2&name=value3


The name of your select can should end with []. For example, <select name="items[]">
Then in your php code, you simply access $_POST['items'][$n]; or count($_POST['items']); or whatever you want to do with the array.

--Dave

0

精彩评论

暂无评论...
验证码 换一张
取 消