coffeescript焦点不是在我的领域

时间:2013-09-12 13:00:05

标签: coffeescript

我想在我的项目中进行客户端验证。 这里的要求是,如果用户提交了空白表单或者他没有记录任何一个输入字段,那么应该生成普通的html列表。 在该列表下,所有输入字段都必须来自谁有错误。 不仅是输入字段,而且它应该是该输入字段的链接。 一旦链接进入该列表,用户就应该能够点击该链接。点击该链接后,他的焦点应该在文本字段上设置。 在这里我可以正确创建链接,但我无法设置焦点。 所以这是我的coffeescript:

$(document).ready ->
# first fetch all input field who have  errors.
  errorElements = $('.content').find('.help-inline').prev()
  ul = $('<ul id="errorlist"></ul>')
  errorElements.each (i) ->
    span = $(this).next()
    labelText = $(this).parent().parent().find('label')
    $(' <li> <a href="#' + $(this).attr('id') + '" class="errorlink" >' + labelText.text() + ' ' + '(' + span.text() + ')' + '</a></li>').appendTo(ul)
  $('#errorcontainer').html(ul)
$(".errorlink").bind "click", (e) ->
  # first I am checking it is binding or not.
  alert('hello')
  $(this).attr(‘href’).setFocus()

我正在使用simple_form作为我的HTML。     所以它正在为我生成html:

  <div class=' content '>


<form accept-charset="

UTF-8 “action =” / levels / basic-computer / topics“class =” simple_form form-horizo​​ntal “enctype =” 多部分/格式数据 “id =”  new_topic“method =”post“novalidate =”novalidate“&gt;

  <div style=" margin:0;padding:0;display:inline ">

<div class=' form-inputs'>
  <div class=" control-group string required topic_title error ">
  <label class=" string required control-label " for=" topic_title ">
  <abbr title=" required ">
  *
  </abbr>
  Title
  </label>
  <div class=" controls ">
  <input class=" string required " id=" topic_title " name=" topic[title] " size=" 50 " type=" text " value="" />
  <span class=" help-inline ">
  can't be blank
  </span>
  </div>
  </div>

我错在哪里?

1 个答案:

答案 0 :(得分:0)

我的印象是只在jQuery对象上使用 .focus()就足够了。

jQuery API Docs: Focus

所以理论上只要这个是正确选择的元素,你只需要这样做:

$(this).focus()

另外我注意到你的绑定代码是从$(document).ready块缩进的。

coffeescript $ - &gt; 中的另一个提示是 $(文档).ready ...

的简写

例如

$ ->
    # do something after the document is ready
相关问题