<table id="tbl1" width="650" border="0" cellspacing="5" cellpadding="0" class="normal-text">
<tr>
<td width="200" align="left" valign="top" class="singleCheckbox">
<input id="rptService_ctl01_rptSubService_ctl01_chkSubService" type="checkbox" name="rptService$ctl01$rptSubService$ctl01$chkSubService" checked="checked" /><label for="rptService_ctl01_rptSubService_ctl01_chkSubService">Energyquote Customer Agreements</label>
</td>
<td width="20" align="left" valign="middle">
<input type="hidden" name="rptService$ctl01$rptSubService$ctl01$hdnServiceid" id="rptService_ctl01_rptSubService_ctl01_hdnServiceid" value="536" />
</td>
<td width="40" align="right" valign="middle">
<select name="rptService$ctl01$rptSubService$ctl01$ddlFrequency" id="rptService_ctl01_rptSubService_ctl01_ddlFrequency">
<option value="1">Daily</option>
<option value="2">Weekly</option>
<option value="3">Monthly</option>
<option value="4">Quarterly</option>
<option value="5">Half Yearly</option>
<option value="6">Yearly</option>
<option selected="selected" value="7">One Time</option>
</select>
</td>
<td width="40" align="right" valign="middle">
<input name="rptService$ctl01$rptSubService$ctl01$txtExpireDate" type="text" id="rptService_ctl01_rptSubService_ctl01_txtExpireDate" class="from" size="8" onKeyDown="preventBackspace();" />
</div>
</td>
<td width="150" align="right" valign="middle">
</td>
</tr>
</table>
$('.normal-text td ').each(function () {
var cellText = $(this).html();
});
上面的代码将获取td标记内的所有元素。但我需要获取td标记内的复选框ID和隐藏字段值。我试过了,但它无法取得
我上面有一个表,这个表有一个类普通文本。我使用下面的代码来获取td中的元素
$('.normal-text td ').each(function () {
var cellText = $(this).html();
});
这里得到了td标签内的整个元素值,但是我想获得td标签内的checkbox id和隐藏值的值。请建议我
答案 0 :(得分:1)
这是如何获得它:
alert("Checkbox is checked: " + $(".normal-text td").find("input[type='checkbox']").prop('checked'));
alert("Hidden field value: " + $(".normal-text td").find("input[type='hidden']").val());
答案 1 :(得分:0)
如果您有多个单一,您可以为此编写逻辑。检查代码段
var checkbox = $('.normal-text td > input[type=checkbox]');
var hidden = $('.normal-text td > input[type=hidden]');
if(checkbox.length>1){
//if you have multiple
checkbox.each(function(){
console.log($(this).is(':checked'));
});
}else {
//if you just have one
console.log(checkbox.val());
}
//similarly ...
hidden.each(function(){
console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbl1" width="650" border="0" cellspacing="5" cellpadding="0" class="normal-text">
<tr>
<td width="200" align="left" valign="top" class="singleCheckbox">
<input id="rptService_ctl01_rptSubService_ctl01_chkSubService" type="checkbox" name="rptService$ctl01$rptSubService$ctl01$chkSubService" checked="checked" />
<label for="rptService_ctl01_rptSubService_ctl01_chkSubService">Energyquote Customer Agreements</label>
</td>
<td width="20" align="left" valign="middle">
<input type="hidden" name="rptService$ctl01$rptSubService$ctl01$hdnServiceid" id="rptService_ctl01_rptSubService_ctl01_hdnServiceid" value="536" />
</td>
<td width="40" align="right" valign="middle">
<select name="rptService$ctl01$rptSubService$ctl01$ddlFrequency" id="rptService_ctl01_rptSubService_ctl01_ddlFrequency">
<option value="1">Daily</option>
<option value="2">Weekly</option>
<option value="3">Monthly</option>
<option value="4">Quarterly</option>
<option value="5">Half Yearly</option>
<option value="6">Yearly</option>
<option selected="selected" value="7">One Time</option>
</select>
</td>
<td width="40" align="right" valign="middle">
<input name="rptService$ctl01$rptSubService$ctl01$txtExpireDate" type="text" id="rptService_ctl01_rptSubService_ctl01_txtExpireDate" class="from" size="8" onKeyDown="preventBackspace();" />
</div>
</td>
<td width="150" align="right" valign="middle"></td>
</tr>undefined</table>