Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
组合jQuery函数调用和if语句?_Jquery_Ruby On Rails_Dynamic_Coffeescript - Fatal编程技术网

组合jQuery函数调用和if语句?

组合jQuery函数调用和if语句?,jquery,ruby-on-rails,dynamic,coffeescript,Jquery,Ruby On Rails,Dynamic,Coffeescript,我对jQuery很陌生,所以这可能是一个严肃的问题,伙计?这类问题,但我有以下情况: dri = $('#drink_restaurant_id :selected').val() $('#drink_restaurant_id').change -> restaurant = $('#drink_restaurant_id :selected').text() ... 我希望它能让应用程序同时检查这两个参数:饮料\餐厅\ id字段上的.change或dri是否有值/是否有效

我对jQuery很陌生,所以这可能是一个严肃的问题,伙计?这类问题,但我有以下情况:

dri = $('#drink_restaurant_id :selected').val()

$('#drink_restaurant_id').change ->
  restaurant = $('#drink_restaurant_id :selected').text()
  ...
我希望它能让应用程序同时检查这两个参数:饮料\餐厅\ id字段上的.change或dri是否有值/是否有效

我试过这样的方法:

$('#drink_restaurant_id').change -> || if dri

if dri || $('#drink_restaurant_id').change ->

(if dri) || ($('#drink_restaurant_id').change ->)
还有其他各种各样的排列,但我的网站上不断出现错误信息

合并这两个检查的最佳方法是什么,这样我就不必为这两个实例重新键入代码了

澄清及守则 我是按照

当您转到新的创建页面时,该程序工作正常,但如果您要编辑现有记录,则餐厅字段“饮料\餐厅\ id”已被选中,但尺寸“饮料\尺寸\ id”和配料“饮料\配料\ id”字段不会根据餐厅字段进行过滤。要解决这个问题,我必须从下拉列表中选择另一家餐厅,然后重新选择原来的餐厅,以便根据大小和餐厅设置过滤器

我自己发现的一项工作是通过以下方式检查餐厅字段是否有价值:

dri = $('#drink_restaurant_id :selected').val()
如果dri有一个值,则执行与某人从下拉列表中选择餐厅并触发.change操作相同的操作

现在,我的代码是这样的。注意代码的重复,因为我想检查两个不同的东西

jQuery ->
  $('.chzn-select').chosen()
  
  sizes = $('#drink_size_ids').html()
  ingredients = $('#drink_ingredient_ids').html()
  
  # Empty out size and ingredients dropdown
  $('#drink_size_ids').html(null).trigger "liszt:updated"
  $('#drink_ingredient_ids').html(null).trigger "liszt:updated"
  
  # When 'editing', will see if restaurant is already selected
  dri = $('#drink_restaurant_id :selected').val()
  
  $('#drink_restaurant_id').change ->
    restaurant = $('#drink_restaurant_id :selected').text()
    size_options = $(sizes).filter("optgroup[label='#{restaurant}']").html()    
    ingredient_options = $(ingredients).filter("optgroup[label='#{restaurant}']").html()    
    if size_options
      $('#drink_size_ids').html(size_options).trigger "liszt:updated"
    else
      $('#drink_size_ids').html($("<option>").attr('selected',true)).trigger "liszt:updated"
      
    if ingredient_options
      $('#drink_ingredient_ids').html(ingredient_options).trigger "liszt:updated"
    else
      $('#drink_ingredient_ids').html($("<option>").attr('selected',true)).trigger "liszt:updated"
      
  if dri
    restaurant = $('#drink_restaurant_id :selected').text()
    size_options = $(sizes).filter("optgroup[label='#{restaurant}']").html()    
    ingredient_options = $(ingredients).filter("optgroup[label='#{restaurant}']").html()    
    if size_options
      $('#drink_size_ids').html(size_options).trigger "liszt:updated"
    else
      $('#drink_size_ids').html($("<option>").attr('selected',true)).trigger "liszt:updated"
      
    if ingredient_options
      $('#drink_ingredient_ids').html(ingredient_options).trigger "liszt:updated"
    else
      $('#drink_ingredient_ids').html($("<option>").attr('selected',true)).trigger "liszt:updated"

问题的解决方案似乎是变量中的一个函数:

# Create a variable that contains a function
updateFields = ->
    restaurant = $('#drink_restaurant_id :selected').text()
    size_options = $(sizes).filter("optgroup[label='#{restaurant}']").html()
    ...    

# Now add this function as the event handler
$('#drink_restaurant_id').change updateFields

# And call it manually if that is indicated
updateFields() if dri

是否出现了支持->的新javascript版本,或者我遗漏了什么@PaoloBergantino咖啡脚本???@Matt:lol!:我只是半认真的,我从没听说过咖啡脚本。有意思。你想做的没有意义。或者只是你解释的方式没有意义。在标准JS语法中,您可以说if dri |$'…'.changefunction{…},但这没有意义,因为.change不检查更改,它分配一个事件处理程序,当字段更改时将调用该事件处理程序。或者您是说在更改时,您希望检索所选选项的值和文本?请详细说明您正在尝试执行的操作?也许描述一下用户可能采取的行动以及接下来应该发生的事情。