ошибка происходит, когда я пробую “весь” метод в datamapper

Необходимо сохранить select элемент disabled, но также и добавить другого скрытого input с тем же именем и значением.

, Если Вы повторно включаете свой ВЫБОР, необходимо скопировать его значение в скрытый вход в onchange событии и отключить (или удалить), скрытый вход.

Вот демонстрация:

$('#mainform').submit(function() {
    $('#formdata_container').show();
    $('#formdata').html($(this).serialize());
    return false;
});

$('#enableselect').click(function() {
    $('#mainform input[name=animal]')
        .attr("disabled", true);
    
    $('#animal-select')
        .attr('disabled', false)
    	.attr('name', 'animal');
    
    $('#enableselect').hide();
    return false;
});
#formdata_container {
    padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <form id="mainform">
        <select id="animal-select" disabled="true">
            <option value="cat" selected>Cat</option>
            <option value="dog">Dog</option>
            <option value="hamster">Hamster</option>
        </select>
        <input type="hidden" name="animal" value="cat"/>
        <button id="enableselect">Enable</button>
        
        <select name="color">
            <option value="blue" selected>Blue</option>
            <option value="green">Green</option>
            <option value="red">Red</option>
        </select>

        <input type="submit"/>
    </form>
</div>

<div id="formdata_container" style="display:none">
    <div>Submitted data:</div>
    <div id="formdata">
    </div>
</div>
6
задан Thilo 4 December 2014 в 18:00
поделиться

1 ответ

Your getting this error because Sinatra takes the return value of a route and converts it into a string before trying to display it to the client.

I suggest you use a view/template to achieve your goal:

# file: <your sinatra file>
get '/show' do
  @comments = Comment.all
  erb :comments
end

# file: views/comments.erb
<% if !@comments.empty? %>
  <ul>
    <% @comments.each do |comment| %>
      <li><%= comment.body %></li>
    <% end %>
  </ul>
<% else %>
    Sorry, no comments to display.
<% end %>

Or append your comments to a String variable and return it when your done:

get '/show' do
  comments = Comment.all

  output = ""
  comments.each do |comment|
    output << "#{comment.body} <br />"
  end

  return output
end
14
ответ дан 8 December 2019 в 17:26
поделиться
Другие вопросы по тегам:

Похожие вопросы: