当用户点击按钮时,我想要执行两种不同的方法。我让他们分开工作,但当我尝试将两者结合起来时,我得到了错误。我目前的两个是
<%= image_tag('go_button.png',
:id => "search_go_button",
:class => "search_go_button",
:onmouseover => "this.style.cursor='pointer';",
:onmouseout => "this.style.cursor='default';",
:onclick => "if ($(\"input_search_field\").value!=\"\" && $(\"input_search_field\").value!=\"Search Places\") {#{
remote_function(:update => "right_nav;",
:url => { :action => :search_places },
:with => "'search_text='+$('input_search_field').value+'&search_radius='+$('radius').innerHTML",
:before => "Element.show('search_spinner'); Element.hide('search_go_button');",
:success => "Element.hide('search_spinner'); Element.show('search_go_button');")
}}") %>
和
:onclick => "collapse_reset(this); new Ajax.Request('/places/search/#{cat.id}?search_radius='+$('radius').innerHTML,{asynchronous:true, evalScripts:true}); {#{remote_function(
:update => "localads;",
:url => { :action => :get_ads,
:id => cat.id },
:before => "Element.show('ad_search_spinner'); Element.show('ad_search_spinner1'); Element.show('ad_search_spinner2'); Element.hide('ad1'); Element.hide('ad2'); Element.hide('ad3'); if($('ad1_slide_down_wrap').style.display != 'none'){$('ad1_slide_down_wrap').style.display = 'none';} if($('ad2_slide_down_wrap').style.display != 'none'){$('ad2_slide_down_wrap').style.display = 'none';} if($('ad3_slide_down_wrap').style.display != 'none'){$('ad3_slide_down_wrap').style.display = 'none';}",
:success => "Element.hide('ad_search_spinner'); Element.hide('ad_search_spinner1'); Element.hide('ad_search_spinner2'); Element.show('ad1'); Element.show('ad2'); Element.show('ad3');") }}",
:href => "/places/navigate/#{cat.id}" } %>
我基本上想要将功能从底部添加到顶部。任何帮助将不胜感激。
答案 0 :(得分:1)
本着UJS的精神(即使你没有在这里提供非javascript功能,你可以从jb中分离javascript),我会使用Low Pro并将它们设置为行为。您需要先下载并包含lowpro.js文件。
在.erb中,您只需拥有图片标记:
<%= image_tag('go_button.png',
:id => "search_go_button",
:class => "search_go_button") %>
在包含的.js文件中,您可能会遇到以下情况:
Event.addBehavior({
'#search_go_button:click' : function(e) {
// stuff you want to have happen in response to a click
},
'#search_go_button:mouseover' : function(e) {
// stuff you want to have happen in response to a mouseover
},
'#search_go_button:mouseout' : function(e) {
// stuff you want to have happen in response to a mouseout
}
});
我们将此用于所有行为,并使代码更易于使用和管理。
如果您需要帮助整理AJAX调用,请发表评论。
答案 1 :(得分:0)
一种(快速)方法是尝试封装这样的函数:
<script type="text/javascript">
function f1() {
if ($(\"input_search_field\").value!=\"\" && $(\"input_search_field\").value!=\"Search Places\") {#{
<%= remote_function(:update => "right_nav;",
:url => { :action => :search_places },
:with => "'search_text='+$('input_search_field').value+'&search_radius='+$('radius').innerHTML",
:before => "Element.show('search_spinner'); Element.hide('search_go_button');",
:success => "Element.hide('search_spinner'); Element.show('search_go_button');")
}} %>
}
function f2() {
collapse_reset(this); new Ajax.Request('/places/search/#{cat.id}?search_radius='+$('radius').innerHTML,{asynchronous:true, evalScripts:true}); {#{ <%= remote_function(
:update => "localads;",
:url => { :action => :get_ads,
:id => cat.id },
:before => "Element.show('ad_search_spinner'); Element.show('ad_search_spinner1'); Element.show('ad_search_spinner2'); Element.hide('ad1'); Element.hide('ad2'); Element.hide('ad3'); if($('ad1_slide_down_wrap').style.display != 'none'){$('ad1_slide_down_wrap').style.display = 'none';} if($('ad2_slide_down_wrap').style.display != 'none'){$('ad2_slide_down_wrap').style.display = 'none';} if($('ad3_slide_down_wrap').style.display != 'none'){$('ad3_slide_down_wrap').style.display = 'none';}",
:success => "Element.hide('ad_search_spinner'); Element.hide('ad_search_spinner1'); Element.hide('ad_search_spinner2'); Element.show('ad1'); Element.show('ad2'); Element.show('ad3');") }}",
:href => "/places/navigate/#{cat.id}" } %>
</script>
和
<%= image_tag('go_button.png',
:id => "search_go_button",
:class => "search_go_button",
:onmouseover => "this.style.cursor='pointer';",
:onmouseout => "this.style.cursor='default';",
:onclick => "f1();f2();" %>
也许您需要将变量传递给函数,但这应该可行