undefinedfix
Sign in

document.getElementById Cannot get value

ypvpi edited in Thu, 13 Oct 2022
 function diqu(){
                    if(document.getElementById("sh").checked)
                    {
                    window.location.replace("https://www.baidu.com");

                    }
                    else if(document.getElementById("hz").checked)
                    {
                     window.location.replace("https://www.runoob.com");
                    }
                    else
                        {alert("您还没有选择所在地区噢!");}
                }
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
  
<div class="cnt">
                <h3>1、您属于哪个区域?</h3>
                <ul>
                    <li><span>&nbsp;</span><label><input type="radio" id="sh" value="" />上海</label></li>
                    <li><span>&nbsp;</span><label><input type="radio" id="hz" value="" />杭州 </label></li>
                </ul>
            </div>
<button onclick="diqu()">试一试</button>


  
</body>
</html>

This is a part of the evaluation form, to enable users to choose Hangzhou and Shanghai to jump to different web pages, but after the selection, it always says "you haven't chosen your region yet!" , it seems that no value has been obtained. Do you have a solution? Assessment link address: http://www.xakj365.com/aa/zlc...

I also have an idea. Is this evaluation selected one by one? In the end, the selection status is not saved, so JS thinks there is no selection?

2 Replies
xtu
commented on Fri, 14 Oct 2022

clipboard.png

This is how to write the HTML structure

<input id="hz" type="radio">
<label for="hz">杭州</label>
<input id="sh" type="radio">
<label for="sh">上海</label>

You are like this now, all are false

clipboard.png

If you can't change it, you can also traverse the. Sel of < Li class = "hover sel" >

If it can help you, you can like it and accept a reward
ChiefGokhlayeh
commented on Fri, 14 Oct 2022

Your own radio shows that the real radio is not checked. You can test it by removing the display none of input