簡體   English   中英

使用 jquery 在按鈕上添加文本單擊到簡單的表單

[英]Adding text on button click to simple form with jquery

我從 jQuery 開始,我正在嘗試添加引導按鈕,這些按鈕在單擊時將西班牙語口音元音添加到 simple_form 文本字段。 我在回答這個問題后對代碼進行了建模,但是單擊按鈕時目前沒有任何反應——我做錯了什么嗎?

在視圖中,我使用簡單的表單並向文本字段添加一個包裝器 id:

<%= simple_form_for @word, url: lesson_image_spelling_path(current_lesson, @word.word), method: :patch do |f| %>
    <%= f.input :spelling_attempt, wrapper_html: { id: 'txt' }, label: "Spell the word:" %>

    <br>

    <button type="button" class="btn btn-secondary btn-sm" id="a-accent">á</button>

    <%= f.button :submit, "Submit", class: 'btn btn-primary' %>
<% end %>

在 application.js 我有:

jQuery("#a-accent").on('click', function() {
  var $txt = jQuery("#txt");
  var caretPos = $txt[0].selectionStart;
  var textAreaTxt = $txt.val();
  var txtToAdd = "test";
  $txt.val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
});

也許使用 input_html 而不是 wrapper_html。 將 console.log('btn clicked') 放入您的點擊事件中,並在瀏覽器控制台中查看此事件是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM