1,Jquery $(this)选择其子对象及复选框是否选中控制,2,find方法的运用

来源:互联网 发布:格式化成json字符串 编辑:程序博客网 时间:2024/05/17 06:24

html

<table >   <thead>        <tr>           <th><input type="checkbox" name="resource" id="selectAll" value=" " />           </th>            <th>序号</th>            <th>资源名称 </th>            </tr>  </thead>       <tbody id ="tBody">              <tr>                  <td>                    <input type="checkbox" />                  </td>                   <td>111</td>                    <td>111</td>                     <td>111</td>              </tr>                  </tbody> </table>

如果现在$(this)代表tbody中的行,现在我想取第一个单元格中复选框的值。可以通过find()来查找。

 var checkBox = $(this).find("input");//得到复选框 //令复选框选中状态与原来相反,其中!是一个亮点。 $(this).find("input").prop("checked",!checkBox.prop("checked")); //添加与移除样式(背景颜色)  if(checkBox.prop("checked")){                    $(this).css("background-color","#337ab7");                }else{                    $(this).removeAttr("style");                }              

此外find 方法里面可以放选择器,可以根据name,id等等查找.
e.g:

//选择$str下name为goodsName的元素$str.find("[name='goodsName']").val("test");或者//选择$str下input中name为goodsName的元素$str.find("input[name='goodsName']").val(item.name_e.id);

实例:

//易制毒品种类数量               var goods = data.precursorInfos;               $.each(goods,function (index,item) {                   $str= $("<tr>" +                           "" +"<td> " +                                       " <select name='goodsName' class='form-control' >"+                                       "<option value='' selected='selected'>请选择品种</option>"+                                       "<c:forEach var='var' items='${requestScope.dangerousType }'>"+                                       "<option value='${var.id}'>${var.keyValue}</option>"+                                       "</c:forEach>"+                                       "</select>" +                                "</td>" +                               "<td>111</td>" +                               "<td>111</td>"+                            "</tr>");                   $("#goodsInfo_update").append($str);                   $str.find("[name='goodsName']").val(item.name_e.id);               })

效果:
这里写图片描述

0 0
原创粉丝点击