此下拉列表值和按钮。单击该按钮时,将删除第一个值,第二个值将为
中的回显 document.getElementById("demo").innerHTML = txt;
<p id="demo"></p>
它的工作正常,但我想得到我的数据库中的行取决于什么是id演示。任何帮助将不胜感激
<!DOCTYPE html>
<html>
<body>
<form>
<?php
require('connection.php');
$positions=mysql_query("SELECT * FROM tbPositions")
or die("There are no records to display ... \n" .
mysql_error());
?>
<td><SELECT NAME="position" id="position" onclick="getPosition(this.value)">
<OPTION VALUE="select">select
<?php
while ($row=mysql_fetch_array($positions)){
echo "<OPTION VALUE=$row[position_name]>$row[position_name]";
}
?>
</SELECT></td>
</form>
<br>
<button onclick="myFunction()">Remove option with index "1"</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("position");
x.options.remove(0);
var x = document.getElementById("position").options.item(0).text;
document.getElementById("demo").innerHTML = x;
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
答案 0 :(得分:0)
如果要在同一页面上显示数据库中的结果,则需要使用AJAX
异步调用以从同一页面上的数据库中获取数据。
所以,比方说,你已经写了一个方法displayResult()
,它发送请求并从数据库中获取结果。您需要在myFunction()
结束时拨打电话,然后从此处传递demo
值。
function myFunction() {
var x = document.getElementById("position");
x.options.remove(0);
var x = document.getElementById("position").options.item(0).text;
document.getElementById("demo").innerHTML = x;
displayResult(x); // calling method to get result.
}
现在,如何编写代码来发出ajax请求?有很多教程和帖子。你可以推荐他们。