簡體   English   中英

無法選擇單選按鈕

[英]Radio buttons cannot be selected

我有些單選按鈕有問題。 以下是用於我的游戲的比賽選擇的代碼。 我實現了錯誤檢測( if (matchType == null) ),但是它給了我一個我不知道的問題。 當我觸發錯誤(不選擇任何單選按鈕)時,文本會正確顯示,但是,此后我將無法選擇任何單選。 就像它鎖定了。 我知道它與onclick事件中的某些事件有關,因為在觸發錯誤之前,我可以選擇它們。 我將不勝感激任何幫助。

PS:我不知道這是否有幫助,但是我將jQuery庫用於單選按鈕。

HTML

    <div id="tabs-1">
    <fieldset>
    <legend>Select a Game type: </legend>
    <legend><b>Normal</b></legend>
    <label for="radio-1">Training</label>
    <input type="radio" class="radioButtons" value="training" name="radios" id="radio-1">
    <label for="radio-2">1v1</label>
    <input type="radio" class="radioButtons" value="one" name="radios" id="radio-2">
    <label for="radio-3">2v2</label>
    <input type="radio" class="radioButtons" value="two" name="radios" id="radio-3">
    <label for="radio-4">3v3</label>
    <input type="radio" class="radioButtons" value="three" name="radios" id="radio-4"><br/>
</fieldset>
</div>

Javascript>

    lobbyDivfindMatch.onclick = function()
{
    if (window.innerWidth >= 1100 && window.innerHeight >= 800)
    {
        var matchType = null;
        //console.log("WIDTH " + window.innerWidth);

        for (var i = 0; i < radios.length; i++)
        {
            if (radios[i].checked)
            {
                matchType = radios[i].value;
                //alert(radios[i].value);
                break;
            }
        }
        if (matchType != null)
        {
            $("#loader").css("display", "inline-block");
            socket.emit('matchMake', {matchType:matchType});
        }
        else if (matchType == null) //Error Detection
        {
            lobbyDiv.innerHTML += "<p>Please select a match type!</p>";
        }
    }
    else
    {
        alert("I'm sorry but you need a screen size of atleast 1100x930 pixels to play!");
    }
}

我想到了!
我只需要將checked =“ checked”添加到第一個單選按鈕。
問題在於所有單選按鈕都未經檢查就開始運行,因此當我單擊按鈕時,它阻止了默認設置。 這樣,除非在inspect元素中編輯了html,否則永遠不會觸發錯誤,因此我將保留錯誤檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM