【问题标题】:jquery type error = 0, functionjquery 类型错误 = 0,函数
【发布时间】:2013-03-02 17:24:15
【问题描述】:

我的document.ready 中有一个函数

  function changeLabel (labelname, newValue) {
    if ($(labelname).html().indexOf("span class") > -1) {
        $(labelname).html("<span class='error'>" + newValue + "</span>");
    } else {
        $(labelname).html(newValue);
    }
  }

但现在我在我的萤火虫中收到一条错误消息:

TypeError: $(labelname).html()null

我不太明白是什么问题,使用函数会是这样的:

changeLabel ("#raffle label[for='first_name']",'Firstname *:');

【问题讨论】:

  • 直接执行 '$("#raffle label[for='first_name']").html().indexOf("span class")' 会发生什么?
  • 如果找不到匹配项,我会说您的选择器。也许也发布相关的 HTML 代码
  • musefan 你说得对,我太傻了,我刚刚添加了 ...if ($('#raffle').length > 0 ) {... 现在它按预期工作了跨度>

标签: jquery function typeerror


【解决方案1】:

将选择器更改为'label[for="first_name"]','#raffle' 这将更改查找标签的位置。如果您仅在 #raffle 中查找标签,请将您的选择器替换为类似的内容。

$(labelname,'#raffle').html();

changeLabel('label[for="first_name"]','new value');

不确定它是否能解决问题。但可能会像你一样选择器可能不正确。

【讨论】:

    【解决方案2】:

    使用 jquery index().. 试试这个

    if ($(labelname).index($("span.error")) > -1) {
        $(labelname).html("<span class='error'>" + newValue + "</span>");
    } else {
        $(labelname).html(newValue);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多