将URL字符串传递给Javascript函数

将URL字符串传递给Javascript函数

问题描述:

I need to pass a URL to a JavaScript function something like the following.

<script type="text/javascript" language="javascript">

var xmlhttp='';   
function ajax()
{
    if(window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
         xmlhttp = new ActivexObject("Microsoft.XMLHTTP");
    }
}     

function someFunction(orders_per_page, url)
{
     ajax();
     var val=document.getElementById("txt_order_amount").value; 
     xmlhttp.onreadystatechange=function()
     {
          if(xmlhttp.readyState==4 && xmlhttp.status==200)
          {                
               document.getElementById("list").innerHTML=xmlhttp.responseText;
          }
     }

     xmlhttp.open("GET",url, true);
     xmlhttp.send();        
}
</script>

<select 
    id="cmb_page" name="cmb_page" 
    onchange="someFunction(this.value, "reports/OrderAmount.php?val="+val+"&orders_per_page="+orders_per_page+"&pageNo="+getSelectedPage();">

    <option value="1">Some Value</option>
</select>

I need to pass a URL string as mentioned on the onchange even of the <select><option></option></select> element. Is it possible?

我需要将URL传递给JavaScript函数,如下所示。 p>

 &lt; script type =“text / javascript”language =“javascript”&gt; 
 
var xmlhttp ='';  
 
函数ajax()
 {
 if if(window.XMLHttpRequest)
 {
 xmlhttp = new XMLHttpRequest(); 
} 
 else 
 {
 xmlhttp = new ActivexObject(“Microsoft.XMLHTTP”)  ; 
} 
 
函数someFunction(orders_per_page,url)
 {
 ajax(); 
 var val = document.getElementById(“txt_order_amount”)。value;  
 xmlhttp.onreadystatechange = function()
 {
 if if(xmlhttp.readyState == 4&amp;&amp; xmlhttp.status == 200)
 {
 document.getElementById(“list”)。innerHTML = xmlhttp  .responseText; 
} 
} 
 
 xmlhttp.open(“GET”,url,true); 
 xmlhttp.send();  
} 
&lt; / script&gt; 
 
&lt; select 
 id =“cmb_page”name =“cmb_page”
 onchange =“someFunction(this.value,”reports / OrderAmount.php?val =“+ val +  “&amp; orders_per_page =”+ orders_per_page +“&amp; pageNo =”+ getSelectedPage();“&gt; 
 
&lt; option value =”1“&gt;某些值&lt; / option&gt; 
&lt; / select&gt; 
   code>  pre> 
 
 

我需要传递 onchange code>上提到的URL字符串,即使是&lt; select&gt;&lt; option&gt;&lt; / option&gt;&lt; / select&gt; code>元素。是否可以? p> div>

try this

onchange="someFunction(this.value, 'reports/OrderAmount.php?val='+val+'&orders_per_page='+orders_per_page+'&pageNo='+getSelectedPage());"

Note: I have replaced double quotes with single quotes

You can also break your code a little bit to prevent as much tag soup.

I assume your val var is defined and in the global scope:

onchange="someFunction(this.value)"

function someFunction(orders_per_page) {
    var url = "reports/OrderAmount.php?val="+val+"&orders_per_page="+orders_per_page+"&pageNo="+getSelectedPage();
    ajax(); // ... rest of your function

Make sure getSelectedPage() is returning a string, or a number which can be parsed as string.