如何从JavaScript循环中获取Select选项的值

时间:2018-08-25 06:39:05

标签: javascript php codeigniter

我想从JavaScript的for-each循环中获取/获取选择选项的值

代码

<?php
include ("conexao.php");
$_SESSION['usu_nome'] = "EDER";
$usuario = $_SESSION['usu_nome'];   
echo "Welcome, ".$usuario."!<p>";
    if (isset($_POST['submit'])) {
    //get file attrivutes
    $name=$_FILES['myfile']['name'];
    $tmp_name=$_FILES['myfile']['tmp_name'];    
    if ($name)
    {
        $location = "avatars/$name";
        move_uploaded_file($tmp_name, $location);       
        $query = mysqli_query("UPDATE esc_usuarios SET imagelocation='$location' WHERE usu_nome ='$usuario'");
        die("Your avatar has benen uploaded");
    }
    else{
        die ("Please select a file!");
    }       
}   
echo "Upload your image:    
<form action='upload.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='myfile'> <input type='submit' value='Upload!'>
</form>
";
?>

2 个答案:

答案 0 :(得分:0)

function myAlert(el){
  alert(el.options[el.selectedIndex].value);
}
 <select onchange="myAlert(this)" class="form-control" name="user" id="user">
            <option value="">Select User to Assign</option>
            <option value="value1">Value 1</option>
            <option value="value2">Value 2</option>
     </select>

答案 1 :(得分:0)

尝试一下。

<?php $user = $this->admin_model->getData('user'); ?>

 <select class="form-control" name="user" id="user" onchange="getValue(this)">
    <option value="">Select User to Assign</option>
       <?php foreach ($user as $row): ?>
           <option value="<?php echo $row->id; ?>"><?php echo $row->user_name;?></option>
       <?php endforeach; ?>
</select>

<script>
function getValue(sel){
   var optionvalue = sel.value;

    alert(optionvalue);

}
</script>