RJS:检查现有的页面元素? [英] RJS: Check for existing page element?

查看:78
本文介绍了RJS:检查现有的页面元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个id为foo的文本字段,有时存在,有时不存在。如果它存在,我想填写一定的价值。

I have a textfield with the id "foo" that sometimes exists and sometimes not. If it exists I'd like to fill in a certain value.

如何使用 RJS (在 Rails 2.2 中)?

我尝试了这个并且它不起作用:

I tried this and it doesn't work:

if page[:foo]
  page[:foo].value = "bar"
end

我得到


TypeError:如果文本字段不存在则为空值

TypeError: Null Value if the textfield doesn't exist

推荐答案

我已经多次出现过我需要的条件项目如果存在则采取行动。使用page.select避免了Null Value错误。

I've come up on this many times where I have a conditional item that needs to be acted upon if it exists. Using page.select avoids the Null Value error.

  page.select('#dom_object_id').each do |element|
    element.value = "bar"
  end

我把它放在我的* .js.rjs文件。当select向每个数组发送一个空数组时,它会返回任何内容而不是当在页面上没有具有正确ID的DOM对象时直接通过id选择时得到的TypeError。

I put this in my *.js.rjs file. When the select sends an empty array to each it returns nothing instead of the TypeError you'd get when directly selecting by id when no DOM object with the correct ID is on the page.

这篇关于RJS:检查现有的页面元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆