0

如何将数组和值传递给 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&agent=Balaji&fromDate=5-8-2013%209:33:6&toDate=10-8-2013%209:33:14

数组的问题。

 How can I build a querystring looks like that?

?fund[]=["PMF","JM"]&agent[]=["Balaji"]&fromDate=5-8-2013%209:33:6&toDate=10-8-2013%209:33:14

基金和代理是数组类型,它取决于用户选择单值或多值。fromDate 和 toDate 是单个值

4

3 回答 3

1

您可以使用此函数从 select 中获取多个选定选项:

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;
于 2013-08-15T06:09:59.043 回答
0

像这样使用序列化的 json 数组并通过 post 发送数据

{

"fund":[
    "value1","value2"
    ],
    "agent":
    ["value1","value2"],
    "fromdate":"value",
    "todate":"value"
    }
于 2013-08-15T05:55:30.947 回答
0

好吧,你可以使用 toString Javascript 函数

http://www.w3schools.com/jsref/jsref_tostring_array.asp

于 2013-08-15T05:15:06.623 回答