你应该确保你的DOM元素进行选择之前至少已经加载。如果你可以使用jQuery,那么这是为下那样容易,你可以把这个脚本在head
部分,或任何地方body
:
$(document).ready(function() {
// Perform any selects on the DOM
});
JS
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
// The DOM is loaded and ready to be selected
var select = document.getElementById("winner");
var optionText = select.options[select.selectedIndex].text;
var optionValue = select.options[select.selectedIndex].value;
});
</script>
中当然,你也可以执行DOM选择使用jQuery:
如果$row
,$team1
或$team2
没有定义,你有PHP错误和通知开启另一种可能性,那么HTML将呈现像这样:
<select name="picks" id="winner"><b>E_NOTICE : </b> type 8 -- Undefined variable: row -- at line 4<br /><b>E_NOTICE : </b> type 8 -- Undefined variable: team1 -- at line 4<br />
<option value=""></option><b>E_NOTICE : </b> type 8 -- Undefined variable: row -- at line 5<br /><b>E_NOTICE : </b> type 8 -- Undefined variable: team2 -- at line 5<br />
<option value="" selected="selected"></option>
</select>
但是,如果你有PHP错误和警告变成关闭,您会看到类似下面的内容:
<select name="picks" id="winner">
<option value=""></option>
<option value="" selected></option>
</select>
如果您无法访问选定HTML中的选项值(因为它们是空的),这将是开始调查的好步骤。
是的,这是可能的,但你想用这个元素做什么? – xNeyte
你究竟是什么意思的“外部”的PHP? –
只需将它分配给一个变量,然后做一些基本的事情,如获得其选定的值 – Marilee