英文:
Display the price of a coin after selecting its name in select
问题
你可以使用以下代码来实现在选择币种后显示其价格:
$(document).on('change', '.my-slim-select', function() {
var selectedCoin = $(this).val();
// 使用Ajax获取数据库中的价格
$.ajax({
url: '/get_coin_price', // 替换成实际的获取价格的路由
type: 'GET',
data: { coin_name: selectedCoin },
success: function(response) {
// 将价格显示在相应的位置
$('#investment_shopping_price').val(response.price); // 替换成实际的显示价格的元素和属性
},
error: function() {
alert('获取价格失败');
}
});
});
请将上述代码添加到你的页面中,并确保以下事项:
- 替换
'/get_coin_price'
成实际获取价格的路由。 - 替换
$('#investment_shopping_price').val(response.price)
成实际显示价格的元素和属性。 - 确保页面中包含jQuery库。
这段代码会在选择框的值发生变化时触发,获取所选币种的价格,并将其显示在相应的位置。
英文:
I use slim-select in which you can select the name of the coin, how to display its price after selecting the name (the price is in the database, i can find it by the name that is selected in the select)How can i do it(maybe i shoud use Ajax but how
)?
My form with select
<%= form_with(model: @investment, local: true) do |f| %>
<div class="">
<p class="very-mb-sm text-center"> <%= f.label :coin_name %></p>
<%= f.select :coin_name, Coin.pluck(:name), {include_blank: true},{data: {controller: 'slim',class:'my-slim-select'}}%>
</div>
<p class="very-mb-sm very-mt-sm text-center"> <%= f.label :shopping_price %></p>
<p class="text-center "><%= f.text_field :shopping_price, class: 'form-text-field'%></p>
<p class="very-mb-sm very-mt-sm text-center"> <%= f.label :invest %></p>
<p class="text-center"><%= f.text_field :invest, class: 'form-text-field'%></p>
<div class="div-center mt-btn">
<%= f.submit '+', class: "text-20" %>
</div>
<% end %>
Update 1
Rendered HTML
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<div class="popup-content">
<form action="/investments" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="u_oVXD2WK9CBd13gQq2i5XSDiEED389yjhY1Pv22sDj2fVz6Y2HJVwj2RRHGgzW9xcXSb78BYmjGFAhmMGOugA" autocomplete="off" />
<div class="">
<p class="very-mb-sm text-center"> <label for="investment_coin_name">Coin name</label></p>
<select data-controller="slim" data-class="my-slim-select" name="investment[coin_name]" id="investment_coin_name"><option value="" label=" "></option>
<option value="Solana">Solana</option>
<option value="TRON">TRON</option>
<option value="Binance USD">Binance USD</option>
<option value="Dai">Dai</option>
<option value="Shiba Inu">Shiba Inu</option>
<option value="Litecoin">Litecoin</option>
<option value="Avalanche">Avalanche</option>
<option value="Uniswap">Uniswap</option>
<option value="Toncoin">Toncoin</option>
<option value="Wrapped Bitcoin">Wrapped Bitcoin</option>
<option value="Monero">Monero</option>
<option value="LEO Token">LEO Token</option>
<option value="TrueUSD">TrueUSD</option>
<option value="Ethereum Classic">Ethereum Classic</option>
<option value="OKB">OKB</option>
<option value="Chainlink">Chainlink</option>
<option value="Filecoin">Filecoin</option>
<option value="Cosmos Hub">Cosmos Hub</option>
<option value="Stellar">Stellar</option>
<option value="Lido DAO">Lido DAO</option>
<option value="Aptos">Aptos</option>
<option value="Bitcoin Cash">Bitcoin Cash</option>
<option value="Cronos">Cronos</option>
<option value="Quant">Quant</option>
<option value="Hedera">Hedera</option>
<option value="ApeCoin">ApeCoin</option>
<option value="The Graph">The Graph</option>
<option value="NEAR Protocol">NEAR Protocol</option>
<option value="Algorand">Algorand</option>
<option value="VeChain">VeChain</option>
<option value="Arbitrum">Arbitrum</option>
<option value="BNB">BNB</option>
<option value="Cardano">Cardano</option>
<option value="XRP">XRP</option>
<option value="Ethereum">Ethereum</option>
<option value="USD Coin">USD Coin</option>
<option value="Tether">Tether</option>
<option value="Polkadot">Polkadot</option>
<option value="Dogecoin">Dogecoin</option>
<option value="Lido Staked Ether">Lido Staked Ether</option>
<option value="Polygon">Polygon</option>
<option value="MultiversX">MultiversX</option>
<option value="Aave">Aave</option>
<option value="Radix">Radix</option>
<option value="Bitcoin">Bitcoin</option>
<option value="Internet Computer">Internet Computer</option>
<option value="Pax Dollar">Pax Dollar</option>
<option value="Fantom">Fantom</option>
<option value="EOS">EOS</option>
<option value="The Sandbox">The Sandbox</option></select>
</div>
<!-- end snippet -->
答案1
得分: 0
我认为如果你在使用CoffeeScript,我可以在这里提供帮助。
我假设你正在使用turbolinks:load来查看你的资产。
你可以创建类似这样的东西:
coinWithPrices: ->
coin = $('#investment_coin_name').val()
$.ajax(url: '/commons/get_prices', data: { coin_name: coin })
.then (data) ->
selected_price = $('#investment_coin_price').data('coin_price')
$(document).on "turbolinks:load", ->
coinWithPrices.init()
这是CoffeeScript的一部分,现在在Rails端做一些工作。
def get_prices
@coin_name = params[:coin_name]
data[:coin_price] = Investment.where("coin_name is ilike '#{@coin_name}'")
render 'investment_form'
end
我假设你的模型名称是Investment,表单名称是investment_form。
我认为这对你会有帮助。
英文:
I think if you are using coffee JS then i can help here.
I am assuming you are using turbolinks:load to view your assets.
You can Create something like this:
coinWithPrices: ->
coin = $('#investment_coin_name').val()
$.ajax(url : '/commons/get_prices', data :{ coin_name: coin })
.then (data) ->
selected_price = $('#investment_coin_price').data('coin_price')
$(document).on "turbolinks:load", ->
coinWithPrices.init()
This is part for coffee js now some work on Rails end.
def get_prices
@coin_name = params[:coin_name]
data[:coin_price] = Investment.where("coin_name is ilike '#{@coin_name}'")
render 'investment_form'
end
I have assume your model name is Investment and your form name is investment_form.
I think this will work for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论