jQuery:根据所选选项显示/隐藏元素
jQuery: show/hide element based on selected option
当选择带有value='Attached File'
的选项时,我想显示带有id=attach
的元素。请帮助我理解为什么我的代码不起作用。
我的代码:
<ul class="form-list">
<li id="excellence-form">
<fieldset>
<ul>
<li class="wide">
<label for="excellence:like" class="required"><em>*</em><?php echo $this->__('Initial Data') ?></label>
<div class="input-box">
<select class="required-entry" name="excellence[like]" id="excellence:like">
<option value='0'><?php echo $this->__('Please Choose..');?></option>
<option value='Not Attached' <?php if($this->getQuote()->getExcellenceLike() == 1){echo 'selected="selected"';} ?>><?php echo $this->__('Do Not Attach File');?></option>
<option value='Attached File' <?php if($this->getQuote()->getExcellenceLike() == 2){echo 'selected="selected"';} ?>><?php echo $this->__('Attach File');?></option>
</select>
</div>
</li>
<li id="attach" style="display:none;" >
<label class="required"><em>*</em><?php echo $this->__('Please Attach :') ?><span id='file_upload_text'></span></label>
<div class="input-box">
<input id="file_upload" type="file" name="file_upload" />
</div>
<input id="file_upload_path" type="hidden" name="file_upload_path" class='required-entry' />
<input type="hidden" value='Attached File' name="file_upload_type" class='required-entry' />
</li>
</ul>
</fieldset>
</li>
</ul>
我的jQuery:
<script type="text/javascript">
$(function() {
$('#excellence:like').change(function(){
if ($(this).val() == "Attached File") {
document.getElementById("attach").style.display="block";
} else {
document.getElementById("attach").style.display="none";
}
});
});
</script>
您应该避免使用":",因为它是css选择器的一部分
试试这个。。。
$(function() {
$('.required-entry').change(function(){ // use class or use $('select')
if ($(this).val() == "Attached File") {
$("#attach").show();
} else {
$("#attach").hide();
}
});
});
您必须退出ID中的:
(或使用select[id='excellence:like']
进行选择):
$('#excellence'':like').change(function(){
if ($(this).val() == "Attached File") {
document.getElementById("attach").style.display="block";
//jQuery alternative
//$("#attach").show();
} else {
document.getElementById("attach").style.display="none";
//jQuery alternative
//$("#attach").hide();
}
});
试试这个
$(function() {
$('#excellence'':like').change(function(){
if ($(this).val() == "Attached File") {
$("#attach").show();
} else {
$("#attach").hide();
}
});
});
相关文章:
- 在jquery中为显示/隐藏设置cookie
- PHP Javascript显示/隐藏按钮不工作
- 打开/关闭按钮以显示/隐藏内容
- 显示隐藏复选框
- 在bootstrap中显示隐藏特定的li
- JQuery上下文菜单显示/隐藏问题
- JQuery在单击时停止显示/隐藏
- HTML5在提交并显示所需标签后显示隐藏的表单元素
- 使用 jQuery 切换显示/隐藏
- 显示/隐藏有关模型更改的指令内容
- Bootstrap在页面加载缓慢时会立即显示隐藏的模式对话框
- 根据特定条件使用ng显示/ng隐藏来显示/隐藏元素
- 显示/隐藏不起作用
- 使用ng-hide根据条件显示/隐藏按钮
- 仅在第一个结果中显示/隐藏MySQL结果函数
- 显示/隐藏http://ftp链接
- 在显示/隐藏中单击时删除的文本
- 是否可以在不重新渲染的情况下显示/隐藏父对象中的元素
- 将页面内容向左移动时显示/隐藏右侧面板
- 显示/隐藏将不会加载