如何制作一个下拉列表,为每个onchange事件提供输出

时间:2017-12-03 09:05:32

标签: javascript jquery html

我正在开发一个下拉选择元素,它有3个选项,可以将它命名为X,Y,Z,其值分别为1,2,3。现在我的问题是如何设置条件或下一步做什么每当我选择选项时显示图表,因为早期它只显示我首先选择的第一个选项的图形,因为已经发生了onchange功能。

2 个答案:

答案 0 :(得分:0)

<select id="ddrp1">
  <option value="">Select an option</option>
  <option value="1">X</option>
  <option value="2">Y</option>
  <option value="3">Z</option>
</select>

假设下拉列表的id为ddrp1。

$("#ddrp1").on("change", function() {
  var selectedVal = this.value;
  var selectedText = this.options[this.selectedIndex].text;
  var selectedVal = $(this).find(':selected').val();
  var selectedText = $(this).find(':selected').text();
  // set your condition here based on selectedText or selectedVal to display graph
}​​​​);​

答案 1 :(得分:0)

试试这段代码。这将为您提供当前选项,您可以根据该选项执行下一步操作。

HTML:

function onOptionChange() {
  console.log($('#select').val());    //prints current option selected
}

JS:

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>Location Tracker</title>
    </head>
    <body>

    <hl> ACW Location Tracker </hl>

<?php
    $server = 'SQL2008.net.dcs.hull.ac.uk';

    $connectionInfo = array("Database"=>"rde_531545");
    $conn = sqlsrv_connect($server,$connectionInfo);
    $query='create table Location ';
    $query .= '(Username int NOT NULL IDENTITY(500, 23), First_Name varchar(50) NOT NULL, Surname varchar(50) NOT NULL, Current_Location varchar(50) NOT NULL, Date datetime NOT NULL, PRIMARY KEY (Username))';
    $result = sqlsrv_query($conn, $query);

    if (!$result)
    {
      if( ($errors = sqlsrv_errors() ) != null)
      {
         foreach( $errors as $error ) {
           echo "<p>Error: ".$error[ 'message']."</p>";
         }
      }
    }
    else {
      echo "<p>DB successfully created</p>";
    }
    sqlsrv_close($conn);

    $connectionInfo = array( "Database"=>"rde_531545");
    $conn = sqlsrv_connect($server,$connectionInfo);
    $insert_query = "INSERT INTO Location (First_Name, Surname, Current_Location, Date) VALUES (?, ?, ?,? )";
    $params = array("John","Doe","Hull", Date);
    $result = sqlsrv_query($conn,$insert_query,$params);
    $params = array("Jane","Doe","Hull", Date);
    $result = sqlsrv_query($conn,$insert_query,$params);

    $LocationQuery='SELECT Username, First_Name, Surname, Current_Location, Date FROM Location';
    $results = sqlsrv_query($conn, $LocationQuery);
    if ($results) while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
    {
       echo '<p>'.$row['Username'].' '.$row['First_Name'].' '.$row['Surname'].' '.$row['Current_Location'].' '.$row['Date'].'</p>';
    }
?>
    </body>
</html>