2015-05-16 60 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(及其所有内容)之后,是这种情况吗? –