2015-05-16 65 views
0
<script type="text/javascript"> 
function radio_form(){ 
    var selectvalue = $('input[name=choice]:checked', '#radio_form').val(); 

if(selectvalue == "V1"){ 
    alert('Value 1'); 
    return true; 
}else if(selectvalue == "V2"){ 
    alert('Value 2'); 
    return true; 
}else if(selectvalue == 'V3'){ 
    alert('Value 3'); 
    return true; 
}else if(selectvalue == 'V4'){ 
    alert('Value 4'); 
    return true; 
} 
return false; 
}; 
</script> 

<form id="radio_form"> 
    <input type="radio" onclick="radio_form()" name="choice" value="V1"> 
    <input type="radio" onclick="radio_form()" name="choice" value="V2"> 
    <input type="radio" onclick="radio_form()" name="choice" value="V3"> 
    <input type="radio" onclick="radio_form()" name="choice" value="V4"> 
</form> 

我試圖在選擇無線電值時顯示警報......但是這種方法似乎不起作用。選擇單選按鈕值時顯示提醒

我做錯了什麼?

+0

如果您正在使用jquery你需要的是($(本).VAL()),並點擊功能 - 演示 - http://jsfiddle.net/z66fbof2/ – Tasos

+0

@TasosAnastasiou我能重定向到一個網址例如? – Borsn

+1

從無線電選擇我假設?是的,將該值更改爲「http://somesite.com」,然後看到下面的答案 – Tasos

回答

1

您應該使用change事件而不是click事件。只有當複選框的值發生變化時,纔會觸發更改事件,即使點擊已選中的點擊事件,每次點擊它時都會觸發點擊事件。另外,不要使用內聯事件,只需使用jQuery選擇器將事件附加到所有複選框。

$("#radio_form input[type=radio]").change(function() { 
 
    alert('Redirecting to: .../' + $(this).val()); 
 
    // This will redirect to the value, relative to the current path 
 
    location.href = $(this).val(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id="radio_form"> 
 
    <input type="radio" name="choice" value="V1"> 
 
    <input type="radio" name="choice" value="V2"> 
 
    <input type="radio" name="choice" value="V3"> 
 
    <input type="radio" name="choice" value="V4"> 
 
</form>

+0

似乎無法讓它正常工作,這就是我所擁有的:「#script type =」text/javascript「> $(」## ('重定向到:... /'+ $(this).val()); //這將重定向到相對的值,到當前路徑 location.href = $(this).val(); }); ' – Borsn

+0

'

\t
\t \t \t
' – Borsn

+0

script標籤應該在'language-container' div(及其所有內容)之後,是這種情況嗎? –