getElementById、单选按钮和 document.write();
单选按钮和 JS 很糟糕。好吧,现在我已经从我的系统中得到了它,这就是我的问题:在阅读 getElementById 与单选按钮配合不好
我可以提醒该值,但 document.write();行不通?
这是我的代码:
<html>
<head>
<script type="text/javascript">
function getRadioValue() {
var y=document.getElementById('draftrequirement_2').value;
document.write(y);
return y;
}
window.onload = function() { alert(getRadioValue()); }
</script>
</head>
<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na" id="draftrequirement_2" />
</body>
</html>
Radio buttons and JS suck. Ok now that I got that out of my system here is my problem: I finally got Javascript to acknowledge the radio button's value after reading getElementById not playing nice with Radio Buttons
I can alert the value but document.write(); won't work?
Here is my code:
<html>
<head>
<script type="text/javascript">
function getRadioValue() {
var y=document.getElementById('draftrequirement_2').value;
document.write(y);
return y;
}
window.onload = function() { alert(getRadioValue()); }
</script>
</head>
<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na" id="draftrequirement_2" />
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Firefox 5 和 Chrome 12 中,我在警报和文档中都看到“na”,因此 document.write() 似乎可以在这些浏览器中工作。但是,在窗口加载事件之后,无线电输入不存在。
我能问你为什么使用 document.write() 吗?操作 DOM 有许多替代方法。来自 w3schools.com (http://www.w3schools.com/js/js_howto.asp)
注意:尽量避免在现实生活中的 JavaScript 代码中使用 document.write() 。如果在函数内部或页面加载后使用 document.write(),整个 HTML 页面将被覆盖。但是,document.write() 是在教程中演示 JavaScript 输出的简单方法。
In Firefox 5 and Chrome 12 I see 'na' in both the alert and the document, so the document.write() seems to work in those browsers. The radio input is not present after the window load event, though.
Can I ask you why you are using document.write()? There are many alternatives to manipulating the DOM. From w3schools.com (http://www.w3schools.com/js/js_howto.asp)
Note: Try to avoid using document.write() in real life JavaScript code. The entire HTML page will be overwritten if document.write() is used inside a function, or after the page is loaded. However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.
不要使用 document.write()。曾经!
让您的生活更轻松,开始使用 jQuery 或类似的库来操作 DOM。
如果您只需要使用纯 javascript 来完成此操作,那么这应该可行:
Do not use document.write(). Ever!
Make your life easier and start using jQuery or similar library for manipulating DOM.
If you need to do it with pure javascript only, this should work:
使用这个它正在工作
Use this it is working