admin管理员组文章数量:1023782
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.
How do you do this by using RJS (in Rails 2.2)?
I tried this and it doesn't work:
if page[:foo]
page[:foo].value = "bar"
end
I get
TypeError: Null Value if the textfield doesn't exist
.
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.
How do you do this by using RJS (in Rails 2.2)?
I tried this and it doesn't work:
if page[:foo]
page[:foo].value = "bar"
end
I get
TypeError: Null Value if the textfield doesn't exist
.
Share Improve this question edited Feb 1, 2016 at 16:53 Sameera R. 4,5922 gold badges39 silver badges53 bronze badges asked Mar 10, 2009 at 14:57 JavierJavier 2,5314 gold badges37 silver badges57 bronze badges2 Answers
Reset to default 6I've e 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
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.
I don't think you can from RJS. As a workaround, you can simply call a javascript method from RJS that checks if the element exists.
page << "update_foo('#{my_value}');"
Javascript ..
function update_foo(val)
{
if( $('foo') != undefined )
{
$('foo').value = 'bar';
}
}
Edit
Fixed the ruby part..
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.
How do you do this by using RJS (in Rails 2.2)?
I tried this and it doesn't work:
if page[:foo]
page[:foo].value = "bar"
end
I get
TypeError: Null Value if the textfield doesn't exist
.
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.
How do you do this by using RJS (in Rails 2.2)?
I tried this and it doesn't work:
if page[:foo]
page[:foo].value = "bar"
end
I get
TypeError: Null Value if the textfield doesn't exist
.
Share Improve this question edited Feb 1, 2016 at 16:53 Sameera R. 4,5922 gold badges39 silver badges53 bronze badges asked Mar 10, 2009 at 14:57 JavierJavier 2,5314 gold badges37 silver badges57 bronze badges2 Answers
Reset to default 6I've e 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
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.
I don't think you can from RJS. As a workaround, you can simply call a javascript method from RJS that checks if the element exists.
page << "update_foo('#{my_value}');"
Javascript ..
function update_foo(val)
{
if( $('foo') != undefined )
{
$('foo').value = 'bar';
}
}
Edit
Fixed the ruby part..
本文标签: javascriptRJS Check for existing page elementStack Overflow
版权声明:本文标题:javascript - RJS: Check for existing page element? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745599757a2158386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论