0

我正在将链接转换为复选框。

我想拿这个

<%= link_to (image_tag "table.png", :alt => "Show Area(s) Table", :style => "padding-left:15px;", :id => "updateAreaTableIconId"), refresh_area_table_path({:field_id => "#{@field.id}"}), :remote => true %> 

并创建这样的东西

<input type='checkbox' id='showAreaBox' onclick='showArea(); <%= redirect_to refresh_area_table_path({:field_id => "#{@field.id}"}), :remote => true %>;'></input>     

所以换句话说我想摆脱链接并将重定向添加到复选框的onclick

如果有办法在 jquery 中触发远程重定向,我可以将它添加到 showArea(); 方法也是。

谢谢!

4

3 回答 3

1

听起来您想在选中复选框时以编程方式单击锚点?

这是一种方法:

var myLink = $('#myLinkId'); // assumes you have a reference to your <a/> element

$('#showAreaBox').change(function() {

    // if the checkbox is checked, invoke the link
    if( this.checked ) {
        myLink.trigger('click');
    }

    // checkbox is unchecked
    // else {}
});
于 2013-07-15T15:13:07.120 回答
0

尝试这个:

JS

$('#showAreaBox').change(function() {
    var el = $(this);
    if (el.prop('checked')) {
        if (el.data('target') == '_blank') {
            window.open(el.data('link'));
        }
        else {
            location = el.data('link');
        }
    }
});

在同一窗口中打开的 HTML

<input type="checkbox" id="showAreaBox" data-link="http://www.example.com">

在新窗口中打开的 HTML

<input type="checkbox" id="showAreaBox" data-target="_blank" data-link="http://www.example.com">
于 2013-07-15T15:26:18.103 回答
0

我是 Web 开发的新手,现在我意识到我没有问正确的问题。
我最终使用了这个

$.post('myLink')

这就是 :remote => true 在 link_to 上所做的。我在这篇文章http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/的 2 上找到了答案

于 2013-07-16T13:09:15.497 回答