1. 程式人生 > >Jquery實現點選某一checkbox時,value類似的checkbox也選中

Jquery實現點選某一checkbox時,value類似的checkbox也選中

<script type="text/jscript">

//點選某checkbox時,把相關的上傳檔案及生成檔案一併刪除。2012.2.15 jb

$(document).ready(function(){

     $(":checkbox").click(function(){

         if(this.checked) {

              var id=$(this).val();

              id=id.substring(0,id.length-4);

              if(id.indexOf("_Original") >=0)

              {

                 id=id.substring(0,id.length-9);

              }

              //alert(id);

              $("input[type='checkbox']").each(

                function() {

                     if($(this).val().indexOf(id) >=0){

                         //alert($(this).val()); 

                         $(this

).attr("checked",true)

                     }

                }

            );

         }

     });

});

</script>

附Jqury對checkbox的常用操作

 jquery 遍歷checkbox jquery 遍歷checkbox
 var a="";
  $('input[type="checkbox"][name="chk"]:checked').each(
                function() {
                       a=a+"|"+$(this).val();  
                }
            );
  $('input[type="checkbox"][name="chk"]').each(
                function() {
                       a=a+"|"+$(this).val();  
                }
            );
 
JQuery操作checkbox、radio例:將多個選中的checkbox的值組裝成一個字串
<script type=text/javascript>
function addMem(){
//var followers = document.getElementsByName("followers");
var f_str = '0';
$("input[@name='followers']").each(function(){
   if($(this).attr("checked")==true){
    f_str += ","+$(this).attr("value");
   }
})
alert(f_str);
}
</script>
=====================
例:取選中的radio的值
var gender = $('input[@name=gender][@checked]').val();
=====================
轉別人的一些東西:
jquery判斷checkbox是否被選中
在html的checkbox裡,選中的話會有屬性checked="checked"。
如果用一個checkbox被選中,alert這個checkbox的屬性"checked"的值alert($"#xxx".attr("checked")),會打印出"true",而不是"checked"!
如果沒被選中,打印出的是"undefined"。覺得很奇怪是嗎?繼續看下去~
不要嘗試去做這樣的判斷:if($"#xxx".attr("checked")=="true")
因為這麼做是錯的,jQuery的API手冊上寫,attr(name)的返回值是object。
所以,應該是if($"#xxx".attr("checked")==true)
====================================
jquery全選/取消選擇checkbox示例:
<input type="checkbox"Double click to hide line number.">

?<script type="text/javascript">
?<!--
?$(function() {
?$("#checkedAll").click(function() {
?if ($(this).attr("checked") == true) { // 全選
?   $("input[@name='checkbox_name[]']").each(function() {
?   $(this).attr("checked", true);
?  });
?} else { // 取消全選
?   $("input[@name='checkbox_name[]']").each(function() {
?   $(this).attr("checked", false);
?  });
?}
?});
?});
?//-->
?</script>
=================================================
jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關獲取一組radio被選中項的值
var item = $('input[@name=items][@checked]').val();
獲取select被選中項的文字
var item = $("select[@name=items] option[@selected]").text();
select下拉框的第二個元素為當前選中值
$('#select_id')[0].selectedIndex = 1;
radio單選組的第二個元素為當前選中值
$('input[@name=items]').get(1).checked = true;
獲取值:
文字框,文字區域:$("#txt").attr("value");
多選框checkbox:$("#checkbox_id").attr("value");
單選組radio:   $("input[@type=radio][@checked]").val();
下拉框select: $('#sel').val();
控制表單元素:
文字框,文字區域:$("#txt").attr("value",'');//清空內容
                 $("#txt").attr("value",'11');//填充內容
多選框checkbox: $("#chk1").attr("checked",'');//不打勾
                 $("#chk2").attr("checked",true);//打勾
                 if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾
單選組radio:    $("input[@type=radio]").attr("checked",'2');//設定value=2的專案為當前選中項
下拉框select:   $("#sel").attr("value",'-sel3');//設定value=-sel3的專案為當前選中項
                $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//新增下拉框的option
                $("#sel").empty();//清空下拉框

$(document).ready(function(){
 $("input[type=checkbox]").click(function(){
  if(this.checked)  //this.checked或this.checked==checked;
  {
   alert($(this).val());
  }
 });
});

jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關獲取一組radio被選中項的值var item = $('input[@name=items][@checked]').val();獲取select被選中項的文字var item = $("select[@name=items] option[@selected]").text();select下拉框的第二個元素為當前選中值$('#select_id')[0].selectedIndex = 1;radio單選組的第二個元素為當前選中值$('input[@name=items]').get(1).checked = true;獲取值:文字框,文字區域:$("#txt").attr("value");多選框checkbox:$("#checkbox_id").attr("value");單選組radio:      $("input[@type=radio][@checked]").val();下拉框select: $('#sel').val();控制表單元素:文字框,文字區域:$("#txt").attr("value",'');//清空內容                    $("#txt").attr("value",'11');//填充內容多選框checkbox: $("#chk1").attr("checked",'');//不打勾                    $("#chk2").attr("checked",true);//打勾                    if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾單選組radio:       $("input[@type=radio]").attr("checked",'2');//設定value=2的專案為當前選中項下拉框select:      $("#sel").attr("value",'-sel3');//設定value=-sel3的專案為當前選中項                   $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//新增下拉框的option                   $("#sel").empty();//清空下拉框// 清空所有複選框選項        $(":checkbox").attr("checked","");
===========================================================以Name獲得值的方法var str="";             $("input[name='newsletter']").each(function(){                   if(this.checked) str+=$(this).val()+",";                         })          alert(str);