无法确定是否已选中单选按钮

时间:2016-03-17 16:34:43

标签: html css radio-button uikit

我在ui-kit切换台组件中使用单选按钮:

<ul data-uk-switcher="{connect:'#availabilityButtons', animation:'fade'}">
     <button class="uk-button uk-button-primary" type="radio" name="perHourButton" id="perHourButton" value="{{this.spaceId}}" data-uk-tooltip title="Rent hourly">Per Hour</button>
     <button class="uk-button uk-button-primary" type="radio" name="'perDayButton" id="perDayButton" namedata-uk-tooltip title="Rent for full days">Per Day</button>
     <button class="uk-button uk-button-primary" type="radio" data-uk-tooltip id="perMonthButton" title="Rent for full months">Per Month</button>

</ul>

我想检查表单提交时检查哪个单选按钮,所以我在前端执行以下操作:

     if (document.getElementById('perHourButton').checked) {
                console.log("perHour Button is checked!!");
            }
            else{
                console.log("not checked!!");
            }

但我继续“未经检查”。我不知道我做错了什么。

2 个答案:

答案 0 :(得分:2)

我害怕&#34;收音机&#34; value对按钮上的type属性无效。这可能是.checked方法没有返回true的原因。尝试使用type =&#34; checkbox&#34;输入来重构你的html。像这样:

C:\>ftp
ftp> open ftp.microsoft.com
Connected to ftp.microsoft.akadns.net.
220 Microsoft FTP Service
User (ftp.microsoft.akadns.net:(none)):
501 Invalid number of parameters.
Login failed.
ftp> user anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230-Welcome to FTP.MICROSOFT.COM. Also visit http://www.microsoft.com/downloads.
230 User logged in.
ftp>

还在有效html的复选框周围添加了li标签。

如果hi是由ui-kit自动生成的(我不知道这个框架),你可能想看看是否可以使用the documentation中的javascript方法检查哪个按钮有被点击(使用jQuery):

<ul data-uk-switcher="{connect:'#availabilityButtons', animation:'fade'}">
     <li>
        <input class="uk-button uk-button-primary" type="checkbox" name="perHourButton" id="perHourButton" value="{{this.spaceId}}" data-uk-tooltip title="Rent hourly" value="Per Hour"/>
     </li>
</ul>

答案 1 :(得分:-1)

试试这个:

if (document.getElementById('perHourButton').checked == true) {
                console.log("perHour Button is checked!!");
            }
            else{
                console.log("not checked!!");
            }