php javascript使用表中的选择值更改innerHTML

时间:2014-09-18 08:58:15

标签: javascript php innerhtml

我有一个带有输入字段(FIELD1)的表单。当用户键入FIELD1时,它会过滤FIELD2中的值。然后他们点击FIELD2,它显示通过javascript选择的值(本教程:http://www.tizag.com/javascriptT/javascript-innerHTML.php)。我遇到的问题是FIELD2的值是表中的companyID - 如何包含javascript以从表中查找公司名称。附图。

<script type="text/javascript">
function changeText2(){
    var userInput = document.getElementById('cv_bus').value;
    document.getElementById('boldStuff2').innerHTML = userInput;
    document.getElementById('filterTxt').innerHTML = userInput;
}
</script>


<table style="width:500px;border:1px solid #000;">
<tr><td>2</td><td>Type in the box below to narrow the list of names:</td></tr>
<tr><td></td><td><input type="text" name="filterTxt" id="filterTxt" tabindex="2" style="width: 400px"></td></tr>
<tr><td></td><td><p>SELECTED: <font color=red><b id='boldStuff2'>NONE</b> </p></font></td></tr>
<tr><td></td><td>Select the apropriate name:</td></tr>
<tr><td></td><td><select autocomplete="off" id="cv_bus" name="cv_bus" size=15 tabindex="2" style="width: 400px" onclick="changeText2()">
<?php 
    include 'cv_con.php';
    $get = mysqli_query($con,"SELECT * FROM cv_companies order by compname");
    $result = mysqli_query($con,"SELECT * FROM cv_companies order by compname");
    //$option = '';

    while($row = mysqli_fetch_array($result)) {
    $option .= '<option value = "'.$row['id'].'">'.$row['compname'].'</option>';
    }
?>
<?php echo $option; ?></select>

enter image description here

2 个答案:

答案 0 :(得分:0)

var el = document.getElementById('cv_bus');
var text = el.options[el.selectedIndex].innerHTML;

尝试此示例,首先获取ID,然后访问选项标签。

答案 1 :(得分:0)

替换此

var userInput = document.getElementById('cv_bus').value;

var userInput = cv_bus.options[cv_bus.selectedIndex].text;

另一种方法是在函数中传递它:

<select autocomplete="off" id="cv_bus" name="cv_bus" size=15 tabindex="2" style="width: 400px" onclick="changeText2(this);">

function changeText2(selObj){
    var userInput = selObj.options[selObj.selectedIndex].text;
    document.getElementById('boldStuff2').innerHTML = userInput;
    document.getElementById('filterTxt').innerHTML = userInput;
}