Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery和CoffeeScript:What';这是检查值是否为表单元格值的子字符串的更优雅的方法 窄客户:-> input=$('.shown_custs')[0]。值 $('.customers\u go\u here tr')。每个(索引,ele)-> 如果ele.cells[0].innerHTML.indexOf(输入) haystack=ele.cells[0]。innerHTML+''+ele.cells[1]。innerHTML 如果haystack.indexOf(输入)_Javascript_Jquery_Coffeescript - Fatal编程技术网

Javascript jQuery和CoffeeScript:What';这是检查值是否为表单元格值的子字符串的更优雅的方法 窄客户:-> input=$('.shown_custs')[0]。值 $('.customers\u go\u here tr')。每个(索引,ele)-> 如果ele.cells[0].innerHTML.indexOf(输入) haystack=ele.cells[0]。innerHTML+''+ele.cells[1]。innerHTML 如果haystack.indexOf(输入)

Javascript jQuery和CoffeeScript:What';这是检查值是否为表单元格值的子字符串的更优雅的方法 窄客户:-> input=$('.shown_custs')[0]。值 $('.customers\u go\u here tr')。每个(索引,ele)-> 如果ele.cells[0].innerHTML.indexOf(输入) haystack=ele.cells[0]。innerHTML+''+ele.cells[1]。innerHTML 如果haystack.indexOf(输入),javascript,jquery,coffeescript,Javascript,Jquery,Coffeescript,这个函数的第四行很难看,我讨厌它。但它是有效的。如果有人能证明这是一种更优雅的方式,我相信这种方式是存在的,那就太棒了 输入是文本框中的值 $('.customers\u go\u here tr')是表行的jQuery数组 我需要知道输入值是否是每行中第一个或第二个td的任何值的子字符串 Wurd.您可以连接两个单元格的innerHTML,然后从字符串中查找输入的索引。它不应该有什么区别,但会使条件语句看起来更好一些(以牺牲一行额外代码为代价): 窄客户:-> input=$('.shown_

这个函数的第四行很难看,我讨厌它。但它是有效的。如果有人能证明这是一种更优雅的方式,我相信这种方式是存在的,那就太棒了

输入是文本框中的值

$('.customers\u go\u here tr')
是表行的jQuery数组

我需要知道输入值是否是每行中第一个或第二个td的任何值的子字符串


Wurd.

您可以连接两个单元格的innerHTML,然后从字符串中查找输入的索引。它不应该有什么区别,但会使条件语句看起来更好一些(以牺牲一行额外代码为代价):

窄客户:->
input=$('.shown_custs')[0]。值
$('.customers\u go\u here tr')。每个(索引,ele)->
haystack=ele.cells[0]。innerHTML+''+ele.cells[1]。innerHTML
如果haystack.indexOf(输入)<0
$(ele.hide()
其他的
$(ele.show()

我很想做这样的事情:

narrow_custs: ->
  input = $('.narrow_custs')[0].value
  $('.customers_go_here tr').each (index, ele) ->
    haystack = ele.cells[0].innerHTML + ' ' + ele.cells[1].innerHTML
    if haystack.indexOf(input) < 0
      $(ele).hide()
    else
      $(ele).show()

所以你只看每行的前两个单元格?为什么?为什么不合并/连接两个单元格的innerHTML,并从该字符串中查找输入的索引?@Bergi这是在用户键入时缩小结果范围(称为keyup)。他们应该能够键入客户机号码(单元格0)或客户机名称(单元格1)以找到他们想要的行(特定的客户)。@AnthonyRoberts:所以这不仅仅是“所有单元格”,对吗?不,不是所有单元格,只有单元格0和单元格1这是目前为止最好的答案,但是我希望有一个我不知道的杀手jQuery方法可以避免使用单元格[0]。innerHTMLAlso,有什么理由添加input=undefined行吗?这能实现什么?这个函数在keyup事件中被调用,所以它总是得到最新的值。“我不是在咆哮,只是真诚地好奇。”安托尼·罗伯茨是我的错。这是我的coffee脚本解析器中的一个bug。请注意,连接不起作用。如果第一个单元格中有“terry”,第二个单元格中有“mun”,那么搜索“ymu”就会找到您所在的行,而这是不应该的。至少在他们之间留一点空间。@Bergi真的!固定的
narrow_custs: ->
  input = $('.narrow_custs')[0].value
  $('.customers_go_here tr').each (index, ele) ->
    haystack = ele.cells[0].innerHTML + ' ' + ele.cells[1].innerHTML
    if haystack.indexOf(input) < 0
      $(ele).hide()
    else
      $(ele).show()
narrow_custs: ->
  input = $('.narrow_custs').val()  # returns the first value
  all = $ '.customers_go_here tr'   # the full set
  show = all.filter (index, ele) -> # the matching ones
    "#{$(ele.cells[0]).html()} #{$(ele.cells[1]).html()}".match input
  hide = all.not show               # the rest
  do hide.hide  # hide the rest
  do show.show  # show the matching ones