简体   繁体   English

使用Javascript取消隐藏元素

[英]Un-hide an element with Javascript

I have two options for select. 我有两个选择选项。 I am trying to get it to where if the user selects "Case Manager" for option for select #1, then option for select #2 appears (it is currently hidden from the user). 我试图让它到达如果用户为选择#1选择“案例管理器”,然后出现选择#2的选项(它当前对用户隐藏)。

Haml: HAML:

= form_tag app_configurations_path, :method=> 'put' do |f|
  -@all_configurations.each do |config|
      =hidden_field_tag "config_ids[]", config.id
      %label= t('workflow.duplicate_claim_manager')
      = select_tag('duplicate_claim[case_manager]', options_for_select(@case_managers_drop_down, config.configuration_value),name: "config[#{config.id}]", :include_blank => true)

      %label.hidden(for="duplicate_claim_manager_secondary")
      = hidden_field('duplicate_claim_manager_secondary', options_for_select(@case_managers_drop_down, config.configuration_value),name: "config[#{config.id}]", :include_blank => true)

The Haml appears to be correct, but I can't get the Javascript to correctly unhide option for select #2. Haml似乎是正确的,但我无法让Javascript正确取消选择#2的选项。 Any ideas? 有任何想法吗?

if ($("#duplicate_claim_case_manager :selected").text() == "Case Manager") {
   $("#duplicate_claim_manager_secondary).setAttribute(type => text)
}

You might consider using jQuery's awesome .toggle() method. 您可以考虑使用jQuery的awesome .toggle()方法。 By itself, it will hide an element if it's currently being shown and show it if it's currently being hidden. 它本身会隐藏一个元素,如果它当前正在显示,如果它当前被隐藏则显示它。 This isn't exactly what you want. 这不完全是你想要的。

But .toggle() also has a secret power. .toggle()也有秘密力量。 You can pass it a boolean as an argument. 您可以将布尔值作为参数传递给它。 If that boolean is truthy, then it will show the element and if it's falsey, it will hide the element. 如果该布尔值是真实的,那么它将显示该元素,如果它是假的,它将隐藏该元素。

You could keep the input as a text field the entire time and just hide it (using the style="display: none;" attribute) by default or with JavaScript on page load. 您可以将输入作为文本字段保留整个时间,并且默认情况下(使用style="display: none;"属性)或在页面加载时使用JavaScript隐藏它。

Then you could use some code like this: 然后你可以使用这样的代码:

var isCaseManager = $("#duplicate_claim_case_manager :selected").text() === "Case Manager";

$("#duplicate_claim_manager_secondary").toggle(isCaseManager);

This line is not right $("#duplicate_claim_manager_secondary).setAttribute(type => text) 这行不正确$("#duplicate_claim_manager_secondary).setAttribute(type => text)

setAttribute syntax: setAttribute语法:

element.setAttribute(name, value); element.setAttribute(name,value);

To change an attribute use: $("#duplicate_claim_manager_secondary).setAttribute("type", "text") 要更改属性,请使用: $("#duplicate_claim_manager_secondary).setAttribute("type", "text")

References: 参考文献:

But to actually hide or show an element, you are better off changing the css display property - with jQuery that is: 但要实际隐藏显示元素,最好更改css display属性 - 使用jQuery:

$("#duplicate_claim_manager_secondary").show()

and

$("#duplicate_claim_manager_secondary").hide() ; $("#duplicate_claim_manager_secondary").hide() ;

I don't know if this was a simple transcription error, but you're missing a closing quote in the following block: 我不知道这是否是一个简单的转录错误,但你在以下块中错过了一个结束语:

This: 这个:

if ($("#duplicate_claim_case_manager :selected").text() == "Case Manager") {
    $("#duplicate_claim_manager_secondary).setAttribute(type => text)
}

Should be this: 应该是这样的:

if ($("#duplicate_claim_case_manager :selected").text() == "Case Manager") {
    $("#duplicate_claim_manager_secondary").setAttribute(type => text); //Added closing quotation mark
}

To make an object visible, remove the display css attribute or set it to your desired mode. 要使对象可见,请删除display css属性或将其设置为所需的模式。 To make an object invisible set its css display attribute to none. 要使对象不可见,请将其css display属性设置为none。

Standard JavaScript: 标准JavaScript:

document.getElementById("duplicate_claim_manager_secondary").style.display = "none"; //Hide
document.getElementById("duplicate_claim_manager_secondary").style.display = "";     //Make Visible

jQuery: jQuery的:

$("#duplicate_claim_manager_secondary").toggle(); // Toggle visibility

//Or...

$("#duplicate_claim_manager_secondary").show(); // Make Visible
$("#duplicate_claim_manager_secondary").hide(); // Hide

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM