【问题标题】:Toggle boolean attribute via checkbox instantly立即通过复选框切换布尔属性
【发布时间】:2012-05-18 00:36:30
【问题描述】:

我的应用中有布尔属性“已完成”的任务。我希望能够选中复选框并立即将该布尔属性从 false 更改为 true(而不必单击提交按钮)。怎么做?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你可以试试:

    <script>        
        function toggle(id, value)
        {
           document.getElementById('img').src = "scriptURL?id=" + id+ "&value=" + value;   } 
     </script>  
    
     <input type="checkbox" name="toggleBox" onclick="toggle(this.id,this.checked);"/>
    
     <img src="scriptURL" id="img" style="display:none;"/> 
    

    图像被隐藏,当您点击复选框时,它会向图像发送一条消息以从“scriptURL”重新加载。

    "ScriptURL" 接收控件的名称和值 - 您的代码可以从查询字符串中提取并处理它们。

    它对用户完全透明,不需要任何额外的框架。

    或者

    <script>
    
        function toggle(id, value)
        {
           var url = "scriptURL?id=" + id+ "&value=" + value; 
    
           // this sends the name and value parameters to the scriptURL
           $("<div/>").html(url);
        } 
    
    
        $(document).ready(function(){
    
            $(".toggleBox").bind("click", function(){
                toggle($(this).attr("id"), $(this).is(':checked'));
            });
        });
    
    </script>
    
    
    <input type="checkbox" id="something" name="something" class="toggleBox" />
    

    Ruby 脚本:

    ...
    require 'cgi' 
    
    params = CGI.parse(request.query_string) 
    # params is now {"id"=>["id name"], "value"=>["true or false"]} 
    
    **Completed = p['value'].first** 
    
    ... rest of your code ...
    

    【讨论】:

    • 好的,但我不明白你写了什么。能否请您澄清或指出一些对菜鸟友好的 AJAX with Rails 教程?
    • 我发布了与此类似的问题,但我在旅行中走得更远...... :) 如果你这么好心,看看它stackoverflow.com/questions/10656860/…
    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多