admin管理员组文章数量:1025208
I want to show element with id=attach
when the option with value='Attached File'
is selected. Please help me to understand why my code does not work.
My code:
<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>
My 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>
I want to show element with id=attach
when the option with value='Attached File'
is selected. Please help me to understand why my code does not work.
My code:
<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>
My 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>
Share
Improve this question
edited Jan 23, 2014 at 11:38
Juniper
asked Jun 27, 2013 at 13:07
JuniperJuniper
7111 gold badge12 silver badges23 bronze badges
2
-
2
#excellence:like
jQuery probably won't like this id, as both CSS and jQuery use:
for pseudo classes and the like. try something like#excellence-like
– Andy Commented Jun 27, 2013 at 13:09 - why are you not using $('#attach') for document.getElementById and not .show() or .hide()? – cptnk Commented Jun 27, 2013 at 13:10
3 Answers
Reset to default 2You should avoid using ":" since it is a part of css selectors
try this...
$(function() {
$('.required-entry').change(function(){ // use class or use $('select')
if ($(this).val() == "Attached File") {
$("#attach").show();
} else {
$("#attach").hide();
}
});
});
You have to escape the :
in your ID (or select with 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();
}
});
try this one
$(function() {
$('#excellence\\:like').change(function(){
if ($(this).val() == "Attached File") {
$("#attach").show();
} else {
$("#attach").hide();
}
});
});
I want to show element with id=attach
when the option with value='Attached File'
is selected. Please help me to understand why my code does not work.
My code:
<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>
My 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>
I want to show element with id=attach
when the option with value='Attached File'
is selected. Please help me to understand why my code does not work.
My code:
<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>
My 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>
Share
Improve this question
edited Jan 23, 2014 at 11:38
Juniper
asked Jun 27, 2013 at 13:07
JuniperJuniper
7111 gold badge12 silver badges23 bronze badges
2
-
2
#excellence:like
jQuery probably won't like this id, as both CSS and jQuery use:
for pseudo classes and the like. try something like#excellence-like
– Andy Commented Jun 27, 2013 at 13:09 - why are you not using $('#attach') for document.getElementById and not .show() or .hide()? – cptnk Commented Jun 27, 2013 at 13:10
3 Answers
Reset to default 2You should avoid using ":" since it is a part of css selectors
try this...
$(function() {
$('.required-entry').change(function(){ // use class or use $('select')
if ($(this).val() == "Attached File") {
$("#attach").show();
} else {
$("#attach").hide();
}
});
});
You have to escape the :
in your ID (or select with 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();
}
});
try this one
$(function() {
$('#excellence\\:like').change(function(){
if ($(this).val() == "Attached File") {
$("#attach").show();
} else {
$("#attach").hide();
}
});
});
本文标签: javascriptjQuery showhide element based on selected optionStack Overflow
版权声明:本文标题:javascript - jQuery: showhide element based on selected option - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614937a2159232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论