2012-02-16 112 views
3

我發現,這樣做的腳本,但他們只用一個單選按鈕的名稱的工作,我有5個不同的單選按鈕組。我如何檢查,如果它現在選擇我現在表格提交Javascript檢查單選按鈕是否被選中?

if(document.getElementById('radiogroup1').value=="") { 
     alert("Please select option one"); 
     document.getElementById('radiogroup1').focus(); 
     return false; 
    } 

不起作用。

+0

的[?我如何檢查單選按鈕是否與JavaScript的選擇(可能的複製http://stackoverflow.com/questions/1423777/how-can-i-籤是否-A-無線電按鈕被選擇的與 - 的JavaScript) – Siraj 2017-03-14 18:09:51

回答

1

這裏有一個很好的教程 - >http://www.somacon.com/p143.php


// return the value of the radio button that is checked 
// return an empty string if none are checked, or 
// there are no radio buttons 
function getCheckedValue(radioObj) { 
    if(!radioObj) return ""; 
    var radioLength = radioObj.length; 
    if(radioLength == undefined) 
     if(radioObj.checked) return radioObj.value; 
     else return ""; 
    for(var i = 0; i < radioLength; i++) { 
     if(radioObj[i].checked) return radioObj[i].value; 
    } 
    return ""; 
} 

// set the radio button with the given value as being checked 
// do nothing if there are no radio buttons 
// if the given value does not exist, all the radio buttons 
// are reset to unchecked 
function setCheckedValue(radioObj, newValue) { 
    if(!radioObj) return; 
    var radioLength = radioObj.length; 
    if(radioLength == undefined) { 
     radioObj.checked = (radioObj.value == newValue.toString()); 
     return; 
    } 
    for(var i = 0; i < radioLength; i++) { 
     radioObj[i].checked = false; 
     if(radioObj[i].value == newValue.toString()) radioObj[i].checked = true; 
    } 
} 
3
var checked = false, radios = document.getElementsById('radiogroup1'); 
for (var i = 0, radio; radio = radios[i]; i++) { 
    if (radio.checked) { 
     checked = true; 
     break; 
    } 
} 

if (!checked) { 
    alert("Please select option one"); 
    radios.focus(); 
    return false; 
} 

return true; 
+3

通過說明書中,頁面上沒有兩個元件應具有相同的ID。 – 2013-03-01 21:03:37

+0

'document.getElementsById()'是錯誤的。 [元素]之後的[** s **]。 它應該是'文檔。getElementById()' OR 'document.getElementsByClassName []()' – Siraj 2017-03-14 18:03:38

0

你沒事用jQuery?如果是這樣的:

$(document).ready(function(){ 
    if($('input[type=radio]:checked').length == 0) 
    { 
     alert("Please select option one");   
     document.getElementById('radiogroup1').focus();   
     return false;  
    } 
} 
+0

'$(document).ready(function(){'使用'$(function(){'後者更新,並且通過更多的跨瀏覽器過濾來運行doc重做檢查,並且更好地適應jquey的風格 – SpYk3HH 2012-02-16 04:34:52

+0

此外,通過簡單的jquery樣式調用if($('input.classnameIsEasier')),if語句在jQuery中變得更加流暢。 (':checked')){/ * do work * /};' – SpYk3HH 2012-02-16 04:38:22

+0

還有一件事,如果你要使用jquery並且需要對無線電和檢查進行表單驗證等,請使用[this plugin]( http://jquery.malsup.com/form/)非常適合爲了更好的功能和更好的驗證時機而添加你的表單 – SpYk3HH 2012-02-16 04:40:19

5

如果你有你的心臟上使用標準的JavaScript然後設置:

函數定義

var isSelected = function() { 
    var radioObj = document.formName.radioGroupName; 

    for(var i=0; i<radioObj.length; i++) { 
     if(radioObj[i].checked) { 
      return true; 
     } 
    } 

    return false; 
}; 

使用

if(!isSelected()) { 
    alert('Please select an option from group 1 .'); 
} 

我會建議使用g jQuery。它有很多選擇器選項,它們在一起使用時將大部分代碼簡化爲單行。

替代的解決方案

if($('input[type=radio][name=radioGroupName]:selected').length == 0) { 
    alert('Please select an option from group 1 .'); 
} 
+1

這個答案是**古代**(在網絡年),所以請注意[jQuery不是最好的解決方案選擇元素](http://blog.garstasio.com/you-dont-ne ed-jquery/selectors /)現在IE <8都是死的。 – BryanH 2016-03-07 20:15:21

1

一個很簡單的功能是:

<script type="text/javascript"> 

function checkRadios(form) { 
    var btns = form.r0; 
    for (var i=0; el=btns[i]; i++) { 
    if (el.checked) return true; 
    } 
    alert('Please select a radio button'); 
    return false; 
} 
</script> 

<form id="f0" onsubmit="return checkRadios(this);"> 
    one<input type="radio" name="r0"><br> 
    two<input type="radio" name="r0"><br> 
    three<input type="radio" name="r0"><br> 
    <input type="submit"> 
</form> 

但是,你前人的精力總是在默認情況下(即與選擇屬性)選擇一個單選按鈕,一些用戶代理可以自動選擇第一個按鈕。然後你只需要檢查默認(通常是第一個)是否被選中。

0

爲什麼不直接使用oneliner?

我寫了這個代碼,它將提交表單如果至少一個無線電檢查:

(function(el){for(var i=el.length;i--;) if (el[i].checked) return el[i].form.submit()||1})(document.form_name.radio_name)||alert('please select item') 

否則就會使警報。或者你也可以修改它與形式的onsubmit使用:

return (function(el){for(var i=el.length;i--;) if (el[i].checked) return 1})(document.form_name.radio_name)||alert('please select item') 

只需更換form_nameradio_name相應。

看看它是如何工作的:http://jsfiddle.net/QXeDv/5/

+0

我知道這是一箇舊的代碼,但是將所有的代碼寫在一行上並不會讓你的代碼成爲'一行'。避免這種做法。 – muhr 2015-12-21 15:47:15

+0

你其實是錯的。他們是可以聯機工作的線索。請看小提琴瞭解他們如何工作。如果你不明白它的作用......那麼最好避免評論。 – Anonymous 2016-06-11 08:07:18

相關問題