无线电值检索

Radio value retrieval

本文关键字:检索 无线电      更新时间:2023-09-26

我不相信有getElementByValue()方法,但有没有办法在我的表单中检索"Yes"值?

<form>
    Do you work at Mori Seiki? <br />
    <input type ="radio" name = "group1_answers" value = "Yes" /> Yes <br />
    <input type ="radio" name = "group1_answers" value = "No" /> No <br />
    <input type ="radio" name = "group1_answers" value = "Perhaps" /> Perhaps <br /> 
    Your answer is: <input type ="text" id = "work_answer" />
    <script>
        if(document.getElementByValue('Yes')){   <-- similar function?
            ;
        }
    </script>
</form>

将属性选择器传递给document.querySelector应该可以做到:

var radioButton = document.querySelector('input[value="Yes"]');