select下拉框的数据是在ajax里面生成的,如何才能取得并上传所选的值
有这么一个情况,下拉框里的选项是后台动态生成的,所以下拉框的代码没有在html里面写,而是写在ajax里面。那这种情况下,我该如何取得用户所选的值并上传到后台某个php中呢?
html的代码(selectdata.html):
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="selectdata.js"></script>
<script type="text/javascript">
$(document).ready(function(){
showselect($("select"));
});
</script>
</head>
<body>
<div id="select" name="select "class="select"></div>
<input type="submit" class="button" value="echo" />
</body>
</html>
function showselect(htmlContainer){
$.ajax({
url:"selectdata.php",
type:"post",
dataType:"json",
success:function(data){
var select = document.createElement("select");
for(var name in data.sex){
var option = document.createElement("option");
option.name = name;
option.innerHTML = name;
option.value = data.sex[name];
select.appendChild(option)
}
document.getElementsByTagName("body")[0].appendChild(select)
}
});
}
<?php
include ("JSON.php");
$json = new Services_JSON();
$arr = array("sex select"=>"","man"=>"male","woman"=>"famale","other"=>"other");
$str = array("sex"=>$arr);
$json = json_encode($str);
echo $json;
?>
[root@localhost select]# php selectdata.php
{"sex":{"sex select":"","man":"male","woman":"famale","other":"other"}}