简体   繁体   English

jQuery显示/隐藏Divs问题

[英]JQuery to show/hide Divs Issue

I am running a SQL query to get dropdown select value. 我正在运行SQL查询以获取下拉选择值。 The values are now int. 现在的值是int。

What I need to do is show the div when Dropdown Value is "Other" 我需要做的是在下拉列表值为“其他”时显示div

So I am using this Jquery code to get the text of the dropdown that is selected. 所以我正在使用此Jquery代码来获取所选下拉列表的文本 Which is working fine. 哪个工作正常。

Working Example Fiddle 工作示例小提琴

<select name="hours" id="hours" class="time">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">Other</option>
<option value="06">06</option>
</select>

Script 脚本

<script type="text/javascript"> 
$('#hours').change(function() {
$('#hours_text').val( this.value );
$('#hours_text2').val($('option:selected',this).text());
});
</script> 

The issue is when I am hiding the div. 问题是当我隐藏div时。 The text is not passing into Jquery. 文本没有传递到Jquery中。 Its only accepting the option value. 它仅接受选项值。 I need it to accept the text value which is "Other" not 05 我需要它接受“其他”而不是05文本

Before the values were varchar so this was not an issue before. 在值是varchar之前,所以这以前不是问题。

Here is the code for hiding and showing Div.(not working) Fiddle 这是隐藏和显示Div。(无效) 小提琴的代码

$('#hours').change(function () {
$('#hours_text').val($('option:selected', this).text());
$('.showotherpDescription').hide();
$('#' + $(this).val()).show.text();
});

Try this: 尝试这个:

$('#hours').change(function () {
    $('#hours_text').val($('option:selected', this).text());
    $('.showotherpDescription').hide();
    $('#' + $('option:selected', this).text()).show();
});

DEMO DEMO

用于检查选项包含的文本的属性将是.html()而不是.val()

Could you do something like this: 你能做这样的事情:

$('#hours').change(function () {
  var sel = $('option:selected', this).text();
  if (sel == "Other") {
    $('.showotherpDescription').show();
  } else {
    $('.showotherpDescription').hide();
  } 
});

Here, this should do the trick. 在这里,这应该可以解决问题。 I added some console output so you can see what and why: 我添加了一些控制台输出,以便您可以了解什么以及原因:

$('#hours').change(function () {
    $('#hours_text').val($('option:selected', this).text());
    $('.showotherpDescription').hide();
    console.log($(this).val());
    console.log(this.selectedIndex);
    console.log(this.options[this.selectedIndex]);
    console.log(this.options[this.selectedIndex].innerHTML);
    $('#' + this.options[this.selectedIndex].innerHTML).show();
});

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

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