2011-06-23 9 views
2

私は自己が行うことになっていますが、ここでjqueryラジオボタンが切り替わらない?

<input name="property" type="radio" checked value="1" />Station Masters House 
<input name="property" type="radio" value="2" />Railway Carriage 

<br><br> 
<div id="fillin">press</div> 

$("#fillin").click(function() {  
     if ($('input[name=property]').val() == "1") 
     { 

      alert($('input[name=property]').val()); 

     } else if ($('input[name=property]').val() == "2") { 

      alert($('input[name=property]').val()); 
     } 


    }); 

例それをしない

http://jsfiddle.net/BFf5H/40/

答えて

4

input[name=property].length === 2 ...ので、.val()ウォン」何を説明に、このコードについて混乱しています仕事。 (.valは、<select multiple>要素を除いて、要求されたセレクタに一致するDOM内の最初の要素の値を取得します。

代わりにinput[name=property]:checkedを試してください。

$("#fillin").click(function() {  
    if ($('input[name=property]:checked').val() == "1") 
    { 

     alert($('input[name=property]:checked').val()); 

    } else if ($('input[name=property]:checked').val() == "2") { 

     alert($('input[name=property]:checked').val()); 
    } 


}); 

いっそのこと、あなただけ(またはこの場合は、jQueryのキャッシュ)DOMをヒットしているので、一度、あなたが必要なものをキャッシュ:

$("#fillin").click(function() { 
    var val = $('input[name=property]:checked').val(); 
    if (val == "1") { 
     alert(val); 
    } else if (val == "2") { 
     alert(val); 
    } 
}); 
1

は、あなたが何をすべき

if ($('input[name=property]:checked').val() == "1")