如何将数组和值传递给javascript函数。 基金和代理人作为一个数组传递。 fromDate和toDate作为单个变量传递。
function fetch_javaScript(fund,agent,fromDate,toDate)
{
-------
-------
var agent = document.getElementById('agent').value;
var fromDate = document.getElementById('fromDate').value;
var toDate = document.getElementById('toDate').value;
var queryString = "?fund=" + fund_name ;
queryString += "&agent=" + agent ;
queryString += "&fromDate=" + fromDate;
queryString += "&toDate=" + toDate;
ajaxRequest.open("GET", "final_ajax.pl" +
queryString, true);
}
<tr><td>
<h2>Funds</h2>
</td>
<td><select name="Funds" id="fund" multiple >
<option value="Select">Select</option>
<option value="PMF">Principal</option>
<option value="PRAMERICA">Pramerica</option>
<option value="JM">JM</option>
<option value="DHL">DHL</option>
<option value="EMF">Edelweiss MF</option>
----
----
</select> <br></td>
</tr>
<tr><td>
<h2>Agents</h2>
</td>
<td><select name="Agents" id="agent" multiple>
<option value="Select">Select</option>
<option value="Gyanesh">Gyanesh</option>
<option value="Satish">Satish</option>
<option value="Sailesh">Sailesh</option>
<option value="ArchanaMuluguru">Archana</option>
----
----
</select></td>
</tr>
我能够将单个变量传递到查询字符串中,就像那样
?fund = PMF&amp; agent = Balaji&amp; fromDate = 5-8-2013%209:33:6&amp; toDate = 10-8-2013%209:33:14
数组问题。
How can I build a querystring looks like that?
?基金[] = [ “PMF”, “JM”]&安培;代理[] = [ “巴拉吉”]&安培; FROM日期= 2013年5月8日209%:33:6和; TODATE = 10-8- 2013%209:33:14
基金和代理是数组类型,它取决于用户选择单值还是多值。 fromDate和toDate是单个值
答案 0 :(得分:1)
您可以使用此功能从选择中获取多个选定的选项:
function get_selected_data(o){
var selectedOptions = [];
for (var i = 0; i < o.options.length; i++){
if(o.options[i].selected){
selectedOptions.push('"' + o.options[i].value + '"');
}
}
if(selectedOptions.length){
return '[]=[' + selectedOptions.join(',') + ']';
} else {
return '';
}
}
例如:
var queryString = "?fund" + get_selected_data(document.getElementById('fund'));
queryString += "&agent" + get_selected_data(document.getElementById('agent'));
queryString += "&fromDate=" + fromDate;
queryString += "&toDate=" + toDate;
答案 1 :(得分:0)
好吧,你可以使用toString Javascript函数
答案 2 :(得分:0)
使用这样的序列化json数组并通过post
发送数据{
"fund":[
"value1","value2"
],
"agent":
["value1","value2"],
"fromdate":"value",
"todate":"value"
}