我想为“在结账时包括上述项目”这一短语做上述主题。当我通过Firebug调试时,脚本中的正确行被击中,但颜色永远不会改变。
有人可以告诉我出了什么问题吗?
HTML
<td colspan="7" valign="middle" align="left" class="underline" style="line-height: 20px;padding: 0; color: #fff;">
<span class="eg_checkbox" style="font-weight:bold;">
<input id="ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit" type="checkbox" name="ctl00$mainContentPlaceHolder$datalistDTO$ctl01$chkShiptoSubmit" checked="checked" onclick="javascript:setTimeout('__doPostBack(\'ctl00$mainContentPlaceHolder$datalistDTO$ctl01$chkShiptoSubmit\',\'\')', 0)"/>
<label for="ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit">Include items above during check out</label>
</span>
</td>
JS
$(document).ready(function () {
if ($('#ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit').is(':checked')) {
$('label[for=#ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit]').css('color', 'red');
}
else {
$('label[for=#ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit]').css('color', 'blue');
}
});
答案 0 :(得分:4)
您不应在属性选择器中使用#
符号,而是多次选择元素,您可以缓存选择器并使用next()
方法选择下一个元素。请尝试以下方法:
$(document).ready(function () {
var $checkbox = $('#ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit');
if ($checkbox.is(':checked')) {
$checkbox.next('label').css('color', 'red');
}
else {
$checkbox.next('label').css('color', 'blue');
}
});
答案 1 :(得分:3)
你的'for'选择器在你的jquery中是错误的:
$(document).ready(function() {
if ($('#ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit').is(':checked')) {
$('label[for="ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit"]').css('color', 'red');
}
else {
$('label[for="ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit"]').css('color', 'blue');
}
});
答案 2 :(得分:1)
这是你的#
。请检查此jsFiddle
注意for=ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit
答案 3 :(得分:0)
HTML:
<table><tr>
<td colspan="7" valign="middle" align="left" class="underline"
style="line-height: 20px; padding: 0; color: #fff;">
<span class="eg_checkbox" style="font-weight:bold;">
<input id="ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit"
type="checkbox"
name="ctl00$mainContentPlaceHolder$datalistDTO$ctl01$chkShiptoSubmit"
checked="checked" />
<label for="ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit">
Include items above during check out
</label>
</span>
</td>
</tr></table>
JS:
$(function()
{
var cb = $(':checkbox');
cb.change(on_cb_changed);
on_cb_changed();
function on_cb_changed()
{
var label = cb.next('label');
label.css('color', cb.is(':checked') ? 'red' : 'blue');
}
});