Php 如何通过javascript验证select
我想用javascript验证select选项,比如如果用户选择Admin,那么这个页面就可以用于Admin登录, 现在,如果用户选择供应商,则选择供应商,而用户选择用户登录Php 如何通过javascript验证select,php,javascript,Php,Javascript,我想用javascript验证select选项,比如如果用户选择Admin,那么这个页面就可以用于Admin登录, 现在,如果用户选择供应商,则选择供应商,而用户选择用户登录 <table class="login_table" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td style="width:30%;">Username</td&g
<table class="login_table" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="width:30%;">Username</td>
<td style="width:70%;"><input name="uname" id="uname" type="text" /></td>
</tr>
<tr>
<td style="width:30%;">Password</td>
<td style="width:70%;"><input name="upass" id="upass" type="password" /></td>
</tr>
<tr>
<td style="width:30%;">Login Type</td>
<td style="width:70%;">
<select>
<option>Admin</option>
<option>Vendor</option>
<option>User</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:right; padding-right:5px;"><input type="submit" class="login_button" name="login" value="Login" /></td>
</tr>
</table>
用户名
密码
登录类型
管理
小贩
使用者
怎么可能可能是这样(没有JS)
管理
小贩
使用者
使用JS
首先,您必须为dropdownlist添加一个id,并添加一个eventhandler
<select id="myId" onchange='Validate()';>
<option>Admin</option>
<option>Vendor</option>
<option>User</option>
</select>
管理
小贩
使用者
然后,使用javascript通过其id访问dropdownlist
<SCRIPT LANGUAGE="javascript">
<!--
function Validate()
{
var dll=document.getElementById("myId")
var myindex = dll.selectedIndex
var SelValue = dll.options[myindex].value
//do something with selected value
}
//-->
</SCRIPT>
或者,您可以使用一个eventhandler,在单击按钮验证DropList时调用该eventhandler
用这个或这个作为你的指南。干杯 在javascript中是这样的
var selectObj = document.getElementById("selObj");
var selectedValue = selectObj.options[selectObj.selectedIndex].value;
if (selectedValue == "Admin")
else
将id分配给select我会使用类似jQuery的lib来简化操作,但本质上是使用jQuery来获取选择,然后将其传递给php,我会使用.change(),.post() http://api.jquery.com/jQuery.post/ 您可以使用change事件查看select上的选定值,然后使用post将数据传递给PHP。如果我现在不是很累,我会用示例代码进一步说明这一点。但我现在要为此而被炒鱿鱼。在那之后,你会使用
window.location=page2go2//根据选择的值来加载每个页面。您的答案可能比其他答案更有用(对于PHP、javascript标记)。但是只需要在HTML部分添加三个
输入
s(如问题中所述)和表单
(问题中缺少)。是的,问题中缺少表单。但我希望他/她会在某个地方发布表单,因为代码中有提交按钮。
var selectObj = document.getElementById("selObj");
var selectedValue = selectObj.options[selectObj.selectedIndex].value;
if (selectedValue == "Admin")
else