-1

テキストフィールド入力があり、データベース属性(ref)の値が含まれています。コードからわかるように、テキストフィールドにフォーカスすると境界線が表示され、クリックすると境界線が消えます。

私の問題は、クリックしたときに、テキストフィールドのデータが送信ボタンなしでデータベースに保存されることを望んでいることです。

<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

特定のレコードを更新するには、blurjqueryを使用してその入力フィールドまたはテキストボックスの関数を呼び出すだけです。入力フィールドのクラス名または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

これは私のコードです:
ビュー上:

    <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}) #cecasは
更新
開始に対応します
@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" endendrescue 例外 => eputs e.message puts e.backtrace.inspect
end redirect_to:action =>'reunion' end
end







tag_formを使用すると、正しく機能しますが、データベースの変更がバックグラウンドで行われるようにしたいと思います

于 2013-03-18T15:31:24.690 に答える
0

ajaxリクエストの送信:http://api.jquery.com/jQuery.ajax/。レコードが存在し、それを更新する場合は「タイプ」を「PUT」にし、新しいレコードの場合は「POST」にします。これを送信するURLは、おそらくあなたが提供したとは思わないコントローラーの名前です。例:「/controller」。

例:

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

それを機能させることができない場合は、使用しているレールコードに関する詳細情報を提供する必要があります。私はいくつかの一般的な推測を行いました。

于 2013-03-15T15:24:06.653 に答える
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 に答える