PHP Ajax传递两个值 - 1个下拉菜单2个按钮

时间:2014-01-13 15:27:25

标签: javascript php html ajax

我有下面的代码适用于下拉菜单但是你可以看到第二个值(next_val = 250f3)是静态的。我希望在点击时改变是动态的。

 <script type="text/javascript">

 function showUser(str)
 {
     if (str == "")
     {
         document.getElementById("txtHint").innerHTML = "";
         return;
     }
     if (window.XMLHttpRequest)
     {
         xmlhttp = new XMLHttpRequest();
     }
     else
     {
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange = function()
     {
         if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
         {
             document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
         }
     }
     xmlhttp.open("GET", "productlistajax.php?q=" + str + "&next_val=250f3", true);
     xmlhttp.send();
}
</script> 

这是下拉菜单代码。

 <select id="maxDaysSinceAdded" name="shorting" onchange="showUser(this.value)">
     <option selected='selected' value="1">Most Recent</option>
     <option value="2">Lowest Price</option>
     <option value="3">Highst Price</option>
 </select>

如何使用按钮在下拉菜单上方传递值

<button type="button" name="buttonpassvalue" onclick="">Value</button>

感谢

1 个答案:

答案 0 :(得分:0)

您正在showUser()的更改事件上致电select tag。因此,在这种情况下,您必须在发送ajax请求之前从脚本本身读取值。

您可以将代码修改为

var buttonValue = document.getElementsByName('buttonpassvalue').item(0).value;
xmlhttp.open("GET", "productlistajax.php?q=" + str + "&next_val=" + buttonValue, true);
相关问题