Why am I having trouble choosing the default radio button on a web page?

It seems like it should be dead simple, but I'm stuck. I wrote some asp.net code that outputs a couple of switches:

<p>
<label for='chkYapper'>Yapper</label>
<input type='radio' name='yapper' id='chkYapper' value='yapper' checked='<%=gblYapperChecked %>' />
<br />
<label for='chkNonYapper'>non-Yapper</label>
<input type='radio' name='Yapper' id='chkNonYapper' value='nonYapper' checked='<%=gblNonYapperChecked %>' />    

        if (registrationUser.isYapper == 1)
        {
            gblYapperChecked = "checked";
            gblNonYapperChecked = "";
        }
        else
        {
            gblYapperChecked = "";
            gblNonYapperChecked = "checked";
        }

As expected, I get two switches: "Yapper" and "Non-Yapper". However, even when I go through my code and see that gblYapperChecked is "checked" and gblNonYapperChecked is "", Non-Yapper is always selected by default in a web browser.

What am I doing wrong?

Refresh Here is the HTML code that actually displays in the browser. "Yapper" should be selected, but "Non-Yapper" is displayed instead.

<p>
<label for='chkYapper'>Yapper</label>
<input type='radio' name='yapper' id='chkYapper' value='yapper' checked='checked' />
<br />
<label for='chkNonYapper'>non-Yapper</label>
<input type='radio' name='yapper' id='chkNonYapper' value='nonYapper' checked='' />    

+3
source share
3 answers

, "checked" HTML . . http://www.w3.org/TR/html401/interact/forms.html#adef-checked .

, , , , checked, checked=true, checked=checked . , checked , , .

​​, :

<input type='radio' name='Yapper' id='chkNonYapper' value='nonYapper' <%=registrationUser.isYapper?"":"checked='checked'" %> />

, isYapper boolean.

+5
+3

Are you setting dblYapperChecked before or after creating the control? Personally, I run the radio buttons on the server side and set the set value in the control directly, but your method should work if the values ​​are set soon enough (try initializing them to the expected values ​​and see if this value changes ...)

0
source

Source: https://habr.com/ru/post/1787754/


All Articles