取 id 值 [英] taking the id value
本文介绍了取 id 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取下面内容的id值?
您选择的值:1您选择的 ID:4
解决方案
你应该做的
document.mainForm.onclick = function(){var radVal = document.mainForm.rads.value;var idVal = document.querySelector('[name="rads"]:checked').id;result.innerHTML = '您选择了:'+radVal;test.innerHTML = '你的id:'+idVal;}
您需要使用 document.querySelector('[name="rads"]:checked')
选择被选中的元素,然后使用 .id
获取 id>
I want to get the id value of the content below?
<form id="mainForm" name="mainForm">
<input type="radio" id="4" name="rads" value="1" checked>
<input type="radio" id="3" name="rads" value="2" />
<input type="radio" id="2" name="rads" value="3" />
<input type="radio" id="1" name="rads" value="4" />
</form>
<span id="result"></span>
<span id="test"></span>
<script>
document.mainForm.onclick = function(){
var radVal = document.mainForm.rads.value;
var idVal = document.getElementsById('id');
result.innerHTML = 'You selected: '+radVal;
test.innerHTML = 'You id: '+idVal;
}
</script>
You selected value: 1 You selected id: 4
解决方案
You should do
document.mainForm.onclick = function(){
var radVal = document.mainForm.rads.value;
var idVal = document.querySelector('[name="rads"]:checked').id;
result.innerHTML = 'You selected: '+radVal;
test.innerHTML = 'You id: '+idVal;
}
You need to select the checked element using document.querySelector('[name="rads"]:checked')
then get the id using .id
这篇关于取 id 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文