2

我想为“结帐时包括上述项目”这句话做上述主题。当我通过 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');
    }
});

4

4 回答 4

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');
    }
});

演示

于 2012-08-02T19:59:48.643 回答
3

您的 jquery 中的“for”选择器错误:

$(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');
    }
});​

演示

于 2012-08-02T19:58:12.843 回答
1

是你的#。检查这个jsFiddle

注意for=ctl00_mainContentPlaceHolder_datalistDTO_ctl01_chkShiptoSubmit

于 2012-08-02T20:13:59.650 回答
0

http://jsfiddle.net/NbVUm/

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');
  }
});
于 2012-08-02T20:02:31.533 回答