0

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?

4

2 回答 2

1

您还可以稍微破坏您的代码,以防止出现过多的标签汤。

我假设您的valvar 已定义并在全局范围内:

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

确保getSelectedPage()返回一个string, 或一个可以解析为字符串的数字。

于 2012-06-02T05:44:30.857 回答
1

尝试这个

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

注意:我已将双引号替换为单引号

于 2012-06-02T05:32:49.163 回答