简体   繁体   English

如何通过javascript验证选择

[英]how to validate select through javascript

I want to validate select option with javascript like if user select Admin then this page work for admin Login, Now if user select vendor then for vendor while user for userlogin 我想使用javascript来验证select选项,例如如果用户选择Admin,则此页面适用于admin登录;现在,如果用户选择了供方,则当供用户登录时,供卖方选择

 <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>

HOw it is Possible 怎么可能

Maybe something like this (without JS)? 也许像这样(没有JS)?

<select name="userType">
 <option value="Admin">Admin</option>
 <option value="Vendor">Vendor</option>
 <option value="User">User</option>
</select>

<?php
// php
if ('Admin' == $_POST['userType']) {
  // work as admin
} else if ('Vendor' == $_POST['userType']) {
  // work as vendor
} else {
  // work as user or any other option
}
?>

USING JS 使用JS

First, you have to put an id for your dropdownlist and add an eventhandler. 首先,您必须为下拉列表添加一个ID,然后添加一个事件处理程序。

<select id="myId" onchange='Validate()';>
    <option>Admin</option>
    <option>Vendor</option>
    <option>User</option>
</select>

Then, access that dropdownlist using javascript via it's id. 然后,使用JavaScript通过其ID访问该下拉列表。

<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>

Or you could use an eventhandler that is invoked when a button is clicked to validate the dropdrownlist. 或者,您可以使用一个事件处理程序,该事件处理程序在单击按钮以验证下拉列表时被调用。

Use this Code Project article or this one as your guide. 使用此代码项目的文章或这一个作为指南。 Cheers! 干杯!

in javascript it will be something like this 在JavaScript中将是这样的

 var selectObj = document.getElementById("selObj");
   var selectedValue = selectObj.options[selectObj.selectedIndex].value;
   if (selectedValue  == "Admin")

   else

Assign id to select 分配ID以选择

I'd use a lib like jQuery just to make it easier but essentially with jQuery to get the selection then pass it to php I would use the .change(), .post() 我会使用像jQuery这样的库只是为了使其更容易,但本质上是使用jQuery来获得选择,然后将其传递给php,我将使用.change()、. post()

http://api.jquery.com/change/ http://api.jquery.com/jQuery.post/ http://api.jquery.com/selected-selector/ http://api.jquery.com/change/ http://api.jquery.com/jQuery.post/ http://api.jquery.com/selected-selector/

You would use the change event to see what the selected value was on the select, then the post to pass the data to PHP. 您将使用change事件查看select上选定的值,然后使用post将数据传递给PHP。 If i wasn't so tired right now I would further this point with example code. 如果我现在不那么累,我将通过示例代码进一步说明这一点。 But I'm to fried for that right now. 但是我现在要为此炒。 After that you would use something like 之后,您将使用类似

window.location = page2go2; window.location = page2go2; //based on value of selected to load whatever page per. //根据选定的值加载每个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM