我有带有选择和选项的表单可供选择。 选择选项值由 asp 经典循环提出。
第二个选择是根据第一个选择值从 javascript 填充的。 然后它通过
发送值var newOption = document.createElement("option");
newOption.value = "2015";
newOption.innerHTML = "Year";
s2.options.add(newOption);
所以现在我想根据已经做出的第二个选择来扩展它将带来 mysql 记录集的位置(还没有提交)
Set package = ObjConn.execute ("Select * from pack_plan where idpack = "JS.value??"")
所以我希望能够接收值并从中选择记录。我不知道这将如何完成。帮助表示赞赏。
最佳答案
以下解决方案对我来说效果很好。 由于您可能不想重新加载页面,因此您需要发出异步请求以根据用户在第一次选择时所做的选择来检索第二次选择的值。 因此,在您的页面上,您将拥有带有选择的表单和几行将进行异步调用的 javascript。
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function jsfunc1(primaryjsvalue){
$.ajax({url:"http://www.yoursite.com/secondary_list.asp?reqvalue="+primaryjsvalue.value, success:function(result){
if (result == "errore") {
$("#thesecondarylist").html('Error');
} else {
$("#thesecondarylist").html(result);
}
}});
}
</script>
<select id="selectid1" name="primaryselect" onchange="jsfunc1(this)">
<option value="val1" id="valid1"> Val1 </option>
<option value="val2" id="valid2"> Val2 </option>
<option value="val3" id="valid3"> Val3 </option>
</select>
<div id="thesecondarylist">
<select id="selectid2" name="secondaryselect" >
<option value="" id=""> Choose from primary list to see available values </option>
</select>
</div>
secondary_list.asp页面会根据主页上选择的选项查询db, 并将循环每个值写入一个响应,该响应将成为“thesecondarylist”div 的内容。
因此将仅包含打开连接、查询数据库并打印从数据库填充的第二个选择选项的代码。
你的asp文件可以直接以:
基本上是你的代码
Set package = ObjConn.execute ("Select * from pack_plan where idpack = "JS.value??"")
成为
Set package = ObjConn.execute ("Select * from pack_plan where idpack = "&Request.QueryString("reqvalue")&"")
'... retrieve your records ...
%>
<select id="selectid2" name="secondaryselect" >
<%
while not rsSecondList.EOF
theValue = rsSecondList("packname")
theId = rsSecondList("idpack")
%>
<option value="<%=theValue%>" id="<%=theId%>"> <%=theValue%> </option>
<%
rsSecondList.Movenext
Wend
%>
</select>
关于MySQL Select 的 Javascript 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29802031/