2014-07-11 139 views
0

沒有更新我在我的表格4個單選按鈕:單選按鈕值在PHP

<tr><td>Type</td><td> 
<input type="radio" name="type" id="a" value="a" >A 
<input type="radio" name="type" id="b" value="b" >B 
<input type="radio" name="type" id="c" value="c" >C 
<input type="radio" name="type" id="d" value="d" >D</td></tr> 

在頁面加載我使用jQuery

$("#b").prop("checked", true); 

設置一個單選按鈕現在我選擇的價值d以我的形式提交。在PHP中,我回顯$ _POST ['type'],我總是得到頁面加載期間使用jquery設置的值,即在這種情況下,b代替d。

爲什麼值沒有更新?

謝謝。

更新:謝謝大家,這是由於無意中val()調用單選按鈕。所以如果使用val()設置單選按鈕的值,它不會在以後改變,奇怪的行爲。

+0

但是,這是負載時,然後我點擊並設置單選按鈕值表格前d在這種情況下,提交 – ak111in

+0

時,有一個提交操作更改頁面負載值到'd'(或提交之前選擇的任何內容)而不是默認的'b'。 – tradyblix

+0

你的代碼的其他部分是否也選擇'b選項'? –

回答

-1

嘗試:用

$("#b").attr("checked", true); 

 $("#b").attr("checked", "checked"); 
+0

沒有任何區別 – ak111in

1

,而不是

$("#b").prop("checked", true); 

你爲什麼不寫你的單選按鈕爲

<input type="radio" name="type" id="a" value="a" >A 
<input type="radio" name="type" id="b" value="b" checked="checked" >B 
<input type="radio" name="type" id="c" value="c" >C 
<input type="radio" name="type" id="d" value="d" >D 
+0

,因爲我正在進行ajax調用[當用戶輸入表單中的第一個字段]以確定需要選擇哪個單選按鈕。這是一個更新頁面,一旦用戶在表單的第一個字段中輸入了他的id,他的詳細信息的其餘部分將填入要編輯和提交的表單中。 – ak111in

1

在jQuery中1.6+

$('#b').prop('checked', true); 
$('#b').prop('checked', false); 

jQuery的1.5及以下

$('#b').attr('checked','checked'); 
$('#b').removeAttr('checked'); 
+0

如果用戶單擊某個其他單選按鈕,是否需要手動刪除已選中的屬性? – ak111in

+0

不,你不需要。按鈕按名稱='類型'分組。 – cb0

+0

爲什麼-1?它用在我的代碼中,像魅力一樣工作。 – cb0

1

就像一個魅力;-))。

<html> 
    <head> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js" /></script> 
     <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#b').attr('checked', 'checked'); 
     }); 
     </script> 
    </head> 

    <body> 
<?php 
if(isset($_POST['sbmt']) && isset($_POST['type'])) { 
?> 
     <h1>Selected type: <?php echo($_POST['type']); ?></h1> 
<?php 
} 
?> 

     <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post"> 
      <ul> 
       <li><input type="radio" name="type" id="a" value="a" /> A</li> 
       <li><input type="radio" name="type" id="b" value="b" /> B</li> 
       <li><input type="radio" name="type" id="c" value="c" /> C</li> 
       <li><input type="radio" name="type" id="d" value="d" /> D</li> 
      </ul> 

      <input name="sbmt" type="submit" value="Submit" /> 
     </form> 
    </body> 
</html> 
+0

謝謝,它工作正常,但沒有在我的情況下工作,也許它與在ajax調用中設置單選按鈕值有關。 – ak111in

+0

@ ak111in你能展示更多的代碼嗎?您的瀏覽器是否正確更新收音機的選擇? – thedom

0

試試這個代碼

<?php 
if(isset($_REQUEST['sb'])) 
{ 
    echo $_REQUEST['type']; 
} 
?> 
<html> 
<body> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script> 
$(document).ready(function(e) { 
    $('#b').attr('checked','checked'); 
}); 
</script> 
<tr><td>Type</td><td> 
<form name="frm" method="post" action=""> 
<input type="radio" name="type" id="a" value="a" >A 
<input type="radio" name="type" id="b" value="b" >B 
<input type="radio" name="type" id="c" value="c" >C 
<input type="radio" name="type" id="d" value="d" >D</td></tr> 
<input type="submit" name="sb" value="submit" /> 
</form> 
</body> 
</html>