我需要编写一些 jquery 来读取下拉列表中的当前值,然后隐藏页面上的特定链接。
下拉列表是一个语言环境选择器。So when I select "english" I want the link to be hidden but when select French the link has to remain.
你能告诉我怎么做吗?我想学这个。谢谢
我需要编写一些 jquery 来读取下拉列表中的当前值,然后隐藏页面上的特定链接。
下拉列表是一个语言环境选择器。So when I select "english" I want the link to be hidden but when select French the link has to remain.
你能告诉我怎么做吗?我想学这个。谢谢
这个怎么样:
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>
开始评估页面非常基本,只是一个标题和指向包含在 start_assessment.html.erb 文件中的主页的反向链接:
<h1>Assessment</h1>
<%= link_to 'Back', :back %>
我这有任何帮助,这里是控制器 home_controller.rb 文件:
class HomeController < ApplicationController
def index
end
def start_assessment
end
end
并且路由 route.rb 文件是:
Rails.application.routes.draw do
root controller: 'home', action: :index
get 'start_assessment' => 'home#start_assessment'
end