Javascript 基于所选下拉列表值隐藏链接
我需要编写一些jquery,它将读取下拉列表中的当前值,然后隐藏页面上的特定链接 下拉列表是一个区域设置选择器。因此,当我选择“英语”时,我希望链接被隐藏,但当选择法语时,链接必须保留 你能告诉我怎么做吗?我想学这个。谢谢这个怎么样:Javascript 基于所选下拉列表值隐藏链接,javascript,jquery,css,locale,aspdotnetstorefront,Javascript,Jquery,Css,Locale,Aspdotnetstorefront,我需要编写一些jquery,它将读取下拉列表中的当前值,然后隐藏页面上的特定链接 下拉列表是一个区域设置选择器。因此,当我选择“英语”时,我希望链接被隐藏,但当选择法语时,链接必须保留 你能告诉我怎么做吗?我想学这个。谢谢这个怎么样: function handleLang() { if ($('#select').val() == 'en') { $('#link').hide(); } else { $('#link').show();
function handleLang() {
if ($('#select').val() == 'en') {
$('#link').hide();
} else {
$('#link').show();
}
}
$(function() {
// hide/show on load
handleLang();
// hide/show when the language is changed (useful if the page doesn't reload)
$('#select').change(handleLang);
});
根据这个问题的第一个答案和第一个答案的作者在评论中提供的更多提示,这里是对我有用的代码的最终(紧凑)完整版本(我也删除了代码,因为不再需要样式)。这是index.html.erb中为此测试应用程序提供的主页
<h2 id='main_header'>Showing/hiding link based on dropdown selection</h2>
<table>
<thead>
<th>Action</th>
<th>Skip assessment</th>
<th>Reason for skipping</th>
</thead>
<tbody>
<tr>
<td>
<%= link_to 'Start asseement', url_for(controller: :home, action: :start_assessment), id: 'start_assessment' %>
</td>
<td>
<%= select_tag "skip_option", options_for_select([ "Yes", "No" ], "No"), id: "skip_option", onchange: "reason_picker()" %>
</td>
<td>
<%= text_field_tag "reason_for_skipping", nil, size: 50, placeholder: 'Enter reason here...' %>
</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready (
window.reason_picker = function () {
var selected = document.getElementById("skip_option").selectedIndex;
if (selected == 0) {
$("#reason_for_skipping").show();
$("#start_assessment").hide();
}
else {
$("#reason_for_skipping").hide();
$("#start_assessment").show();
}
}
);
</script>
这似乎是可行的,但我的李没有为它指定一个类。。如何检查特定链接:这是我的意思的更新,我想我需要检查特定链接。-我已经更新了您的示例以隐藏某个链接。可以吗?
<h1>Assessment</h1>
<%= link_to 'Back', :back %>
class HomeController < ApplicationController
def index
end
def start_assessment
end
end
Rails.application.routes.draw do
root controller: 'home', action: :index
get 'start_assessment' => 'home#start_assessment'
end