簡體   English   中英

如何切換內容?

[英]How to toggle content of <td>?

我非常希望能夠在有 x 和沒有 x 之間切換

<td class="checkbox"> <input name="signed" type="checkbox" checked /> x </td>

例如,參見 JSFiddle

http://jsfiddle.net/littlesandra88/GMSCW/

按下提交時觸發的代碼是

$('form').live('submit', function(){
   $.post($(this).attr('action'), $(this).serialize(), function(response){
         // do something here on success
   },'json');
   return false;
});

我想我必須做一些事情

$(this).getElementByClass("checkbox").td.toggle('x');

但是checked還是應該在<input name="signed" type="checkbox" checked />中更新

有誰知道如何做到這一點?

將 X 包裹在一個跨度中,然后隱藏它

<td class="checkbox"> 
      <input name="signed" type="checkbox" checked />
      <span id='toggleme'>x</span>
 </td>

 //change the visibility of the x
 $('#toggleme').toggle();

// check the checkbox
$('input[name=signed]').attr('checked', true);

要檢查 jQuery 中的復選框,請使用ELEMENT.attr("checked","checked")或 jQuery 1.6 方式和ELEMENT.prop("checked", true)

最好的主意,IMO,是在復選框旁邊放一個<label> ,然后用$("input[type=checkbox] + label", this).toggle() toggle它。

要更改復選框,其他用戶提供的示例可以正常工作。

http://jsfiddle.net/w5nAr/

這假設您不想更改復選框,只更改它旁邊的文本。 如果你想要兩者,這是另一個例子:

http://jsfiddle.net/w5nAr/4/

嘗試查看這里的代碼,它基於 Bryan 的工作。

$({some identifier for your checkbox}).attr("checked",true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM