English 中文(简体)
铁路、 j和修路
原标题:Rails, javascript and updating forms

我的表格允许用户选择一个国家。 视选定哪个国家而定,我需要改变国家/各省的辍学率,以列入各州或各省名单。 我正在使用<代码>observe_field tag进行这项工作,但这在铁路3号。

因此,现在应该如何做到这一点? 我使用的是select_tag,以填平退数和options_for_select中使用的阵列。 所有的服务器都储存在控制器操作中,因此,我可以从 j带上。

最佳回答

https://github.com/jim/carmen"rel=“nofollow” https://github.com/jim/carmen

I did the following some times ago (AJAX).

传真:

    <p>
        <label>Country <span>*</span></label>
        <%= profile_form.select(:country,Carmen.countries, {:include_blank =>  Select a Country }, :id => "profile_country") %>
    </p>
    <p>
        <label>State <span>*</span></label>
        <%= profile_form.select(:state,  "" , {:include_blank =>  Select a Country first }, :id => "profile_state") %>
    </p>

主计长:

def states
  begin
    render :json => Carmen::states(CGI::unescape(params[:country]))
  rescue 
    render :json => {"content" => "None"}.to_json
  end
end 

Javascript with jQuery:

$( #profile_country ).change(function() {
    if ($(this).val() ==   )
    {
      $( #profile_state ).empty();
      $( #profile_state ).append( $( <option>No state provided for your country</option> ));
    }
    else {
        $.ajax({
            type: "GET",
            url: "/remote/get_states/" + encodeURIComponent($(this).attr( value )),
            success: function(data){
                if (data.content ==  None )
                {
                    $( #profile_state ).empty();
                  $( #profile_state ).append( $( <option>No state provided for your country</option> ));
                }
                //handle the case where no state related to country selected
              else
             {
                $( #profile_state ).empty();
                $( #profile_state ).append( $( <option>Select your State</option> ));
                jQuery.each(data,function(i, v) {
                 $( #profile_state ).append( $( <option value=" + data[i][1] + "> +data[i][0] + </option> ));
            });
        }
        }
      });
    }
 });
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签