-1

我有一个文本字段输入,它包含数据库属性(参考)的值。从代码中可以看出,当我关注文本字段时,会出现边框,当单击 out 时,边框会消失。

我的问题是,当我点击时,我希望文本字段中的数据将存储在数据库中,而不需要提交按钮。

<script>
$(document).ready(function(e){

    $('.class1').focusin(function(){
        $(this).attr('readonly',false);
        $(this).css('border','1px black solid');
    })

    $('.class1').focusout(function(){
        $(this).attr('readonly',true);
        $(this).css('border','0px white solid');

    })
} );
</script> 

<div>Ref: <%= text_field_tag(:ref,@ref.to_s,:readonly=>true, :class => "classe1" )%>  </div>

如何将值发送到我的应用程序?

4

4 回答 4

1

要更新特定记录,您只需blur使用 jquery 为该输入字段或文本框调用函数。使用输入字段的类名或 id 调用 blur 函数。

$('#ref').blur(function() {
    update_ref_field();
});

调用ajax的JS函数:

function update_ref_field(){
    var ref_value = $('#ref').val();
    var url = '/controller_name/action_name/?ref='+ref_value;
    $.ajax({
       type: 'put',
       url: url,
       dataType: "jsonp",  
       // You can use this jsonp if your request related to cross domain
       error: function (result, status, xhr){
            alert('result='+result+'::status='+status+'::xhr= '+ xhr);
            alert('Error occurred while updating the record.');           },
       success: function(result, status, xhr){
           alert('result='+result+'::status='+status+'::xhr= '+ xhr);
           alert('Record updated successfully.');

    });
    return false;
}

更新代码

function update_ref_field(){
  var ref_value = $('#ref').val();
  var url = '/controller_name/action_name/?ref='+ref_value;
  $.ajax({
    type: 'post',
    url: url,
    dataType: 'script',
    error: function(data){
      alert('Error occurred while updating the record.');
    }, 
    success: function(data){
      alert('Record updated successfully.');
    });
    return false;
}
于 2013-03-18T06:25:43.807 回答
0

Sending an ajax request: http://api.jquery.com/jQuery.ajax/. Make the "type" a "PUT" if the record exists and you're updating it, and a "POST" if it's a new record. The url to send this to is most likely the name of the controller which I don't think you provided. e.g. "/controller".

Example:

$.ajax({
  type: "POST",
  url: "/controller",
  data: {ref: $('.classe1').html().trim()}
});

If you can't get it working you'll need to provide some more information about the rails code you're working with, I took a few generic guesses.

于 2013-03-15T15:24:06.653 回答
0

这是我的代码:
在视图中:

    <script> 
     function update_ref_field(){
     var ref_value = $('#ref1').val();
     var url = '/ModController/update_ref_field/?ref1='+ref_value;
    $.ajax({
        type: "POST",
        url: url,
        //dataType: "jsonp",
        // You can use this jsonp if your request related to cross domain
        error: function (result, status, xhr){
            alert('result='+result+'::status='+status+'::xhr= '+ xhr);
            alert('Error occurred while updating the record.');           },
        success: function(result, status, xhr){
            alert('result='+result+'::status='+status+'::xhr= '+ xhr);
            alert('Record updated successfully.');
        }});
       return false;
       }


        $('#ref1').focusin(function(){
            $(this).attr('readonly',false);
            $(this).css('border','1px black solid');
        })

        $('#ref1').focusout(function(){
            $(this).attr('readonly',true);
            $(this).css('border','0px white solid');
            $javascript:update_ref_field();


    })
} );

在控制器上:
def update_ref_field()
begin
@projj=Project.find(params[:project_id]) #projet courant
@local_date = Time.new().to_date
@sem = caluculer_semaine(@local_date)
@existe_mom_pour_cet_semaine = Mod.find( :all, :conditions => {:project_id => @projj.id , :semaine => @sem})
#ce cas 对应一个 un update
begin
@existe_mom_pour_cet_semaine.each do |a|
if (params[:ref1]) != "" || (params[:ref1]) != nil
a.update_attributes(:ref => params[:ref1])
end
if a.save
flash[:notice] ="mise a jour de ref "
else
flash[:error] ="mise ajour fr ref non aboutit"
end
end
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
redirect_to :action=>'reunion'
end
end

当我使用 tag_form 时,我可以正常工作,但我希望数据库上的更改将在后台进行
谢谢

于 2013-03-18T15:31:24.690 回答
0

感谢你的帮助。
这个问题的解决方案是使用一个javascript函数来调用控制器中的另一个函数

 function loadXMLDoc(id, val, name)
{

    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            alert();
        }
    }

            //update_ref_field => methode in the contoller with complete the update in the data     base
            xmlhttp.open("GET","update_ref_field?ref1="+val ,true);
            xmlhttp.send();


}
于 2013-04-11T10:45:57.417 回答