javascript value is null
javascript value is null
好了,我完全迷路了,我是一个初学者。
我的一个朋友有一个广播网站,它是一个非常古老的网站。
有一个可以更新用户的表单。
这个表单有一个名为dj的单选按钮,值为yes或no。
在索引页上显示了用户列表
和我想做的,如果这个单选按钮被选中,那么在用户名之后添加一个小的DJ图像。
但是无论我做什么我总是得到null值
页眉和页脚都包含在这两个文件,如果我是对的,假设给结果没有?我在页脚放置它返回null作为结果
var dj = document.getElementById("dj");
alert(dj.value);
我试了试窗户。Onload函数什么也没发生
我真的很想在javascript中做这个练习。
有人可以解释我错过了什么,为什么我不能得到这个显示在索引上?有可能做到吗?
请温柔一点,我是个初学者
Try
alert(dj.checked);
如果你创建了你的单选按钮微笑器,它将根据单选按钮的状态报警为真或假
<input type="radio" name="music" value="test" id="dj">
我已经创建了一个工作示例(虽然不是很优雅),它确切地说明了由于您提到您是初学者,需要做什么。
http://sente.cc/misc/radio_button_test.html代码是:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>radio buttons</title>
<script>
var testit = function(){
var radioyes = document.getElementById('idyes');
var radiono = document.getElementById('idno');
if (radioyes.checked){alert(radioyes.value);}
if (radiono.checked){alert(radiono.value);}
};
</script>
</head>
<body>
<form>
DJ:
<br>
<input id="idyes" type="radio" name="sex" value="yes" /> Yes<br />
<input id="idno" type="radio" name="sex" value="no" /> No
</form>
</body>
<span style="background:lightblue;" onclick="javascript:testit()">click here to run javascript</span>
</html>
有更好的方法去做这件事。(比如遍历所有的无线电元素,直到你遇到一个.checked
属性为true
的元素)
可能两个单选按钮有相同的ID,所以这样的代码不能工作。
可以这样做:(假设你只有一个表单)
var oForm = document.forms[0];
var value = "";
for (var i = 0; i < oForm.elements.length; i++) {
var el = oForm.elements[i];
if (el.type == "radio" && el.name == "dj" && el.checked) {
value = el.value;
break;
}
}
alert("selected value: " + value);
代码非常简单。
实时测试用例
我将引用jQuery库并使用以下代码:
$(document).ready(function() {
alert($("#dj").val());
});
相关文章:
- contentWindow.document.body is null
- document.getElementById(..) is null
- jquery TypeError: Response is null
- iframe contentDocument and contentWindow is null
- document.querySelector(...) is null error
- getElementById is null?
- Sbt 0.13 ScriptEngine is Null for getEngineByName(“JavaScrip
- AngularJS "localStorage.getItem" is null in $resou
- javascript canvas is null
- Sails.create even when item is null?
- Jquery ajax POST response is null
- Google App Engine: User is null
- Microsoft.XMLHTTP documentElement is NULL
- TypeError: this.element is null, Rails with Judge gem
- FileReader.readAsArrayBuffer return is null
- TypeError: jasmine.getEnv().currentSpec is null
- gapi.auth.authorize: TypeError: cv(...) is null
- Error document.getElementById is null
- Javascript TypeError: document.getElementById(...) is null
- JavaScript :TypeError: document.getElementById(...) is null