getElementById、单选按钮和 document.write();

发布于 2024-11-27 21:36:58 字数 714 浏览 8 评论 0原文

单选按钮和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-12-04 21:36:58

在 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.

白龙吟 2024-12-04 21:36:58

不要使用 document.write()。曾经!

让您的生活更轻松,开始使用 jQuery 或类似的库来操作 DOM。

如果您只需要使用纯 javascript 来完成此操作,那么这应该可行:

<html>
<head>
<script type="text/javascript">
function checkRadio() {
var y=document.getElementById('draftrequirement_2').value;
document.getElementById('draftrequirement_2_message').innerHTML = y;
}

</script>
</head>

<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na"   id="draftrequirement_2" />
<div id="draftrequirement_2_message" />
</body>
</html>

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:

<html>
<head>
<script type="text/javascript">
function checkRadio() {
var y=document.getElementById('draftrequirement_2').value;
document.getElementById('draftrequirement_2_message').innerHTML = y;
}

</script>
</head>

<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na"   id="draftrequirement_2" />
<div id="draftrequirement_2_message" />
</body>
</html>
落叶缤纷 2024-12-04 21:36:58
<html> 
<head>
<script type="text/javascript">
function checkRadio() {
var y=document.getElementById('draftrequirement_2').value;
document.write(y);
return y;
}


</script>
</head>

<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na"     id="draftrequirement_2" />NA
</body>
</html>

使用这个它正在工作

<html> 
<head>
<script type="text/javascript">
function checkRadio() {
var y=document.getElementById('draftrequirement_2').value;
document.write(y);
return y;
}


</script>
</head>

<body>
<input onchange="checkRadio()" type="radio" name="draftrequirement" value="na"     id="draftrequirement_2" />NA
</body>
</html>

Use this it is working

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文