This is quite frustrating. My code was working fine until last week. I am adding multiple textboxes to my HTML page when the user changes [using change()
] the value in a dropdown selection.
Here's the HTML code snippet:
<div id="files" class="control-group">
<label class="control-label">No. of files</label>
<div class="controls" >
<select id="files" name="files" class="span3">
<option value="">--Select Option--</option>
<?php for($i=1;$i<21;$i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
</select>
</div>
</div>
<div class="control-group" id="titles">
<label class="control-label">File Titles</label>
<div class="controls" id="titleAdd"></div>
</div>
Here's my Javascript / jQuery:
$(document).ready(function(){
$("#titles").hide();
});
var container;
// Add & Remove textboxes
$("#files").change(function(){
//Remove all textboxes
$("#titles").show();
$(container).empty();
$(container).remove();
//"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
container = $('<div>', {class: 'controls'});
var option = $("#files").val();
for(i=1;i<=option;i++)
{
// Add a TextBox
$(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
}
$('#titleAdd').after(container); // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
});
The most irritating part is that this code was working fine a few days ago.
This is quite frustrating. My code was working fine until last week. I am adding multiple textboxes to my HTML page when the user changes [using change()
] the value in a dropdown selection.
Here's the HTML code snippet:
<div id="files" class="control-group">
<label class="control-label">No. of files</label>
<div class="controls" >
<select id="files" name="files" class="span3">
<option value="">--Select Option--</option>
<?php for($i=1;$i<21;$i++){ ?>
<option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
</select>
</div>
</div>
<div class="control-group" id="titles">
<label class="control-label">File Titles</label>
<div class="controls" id="titleAdd"></div>
</div>
Here's my Javascript / jQuery:
$(document).ready(function(){
$("#titles").hide();
});
var container;
// Add & Remove textboxes
$("#files").change(function(){
//Remove all textboxes
$("#titles").show();
$(container).empty();
$(container).remove();
//"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
container = $('<div>', {class: 'controls'});
var option = $("#files").val();
for(i=1;i<=option;i++)
{
// Add a TextBox
$(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
}
$('#titleAdd').after(container); // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
});
The most irritating part is that this code was working fine a few days ago.
Share Improve this question edited Jun 11, 2015 at 11:58 Anthon 77.3k34 gold badges204 silver badges281 bronze badges asked Jun 11, 2015 at 5:23 Shreyas TripathyShreyas Tripathy 3257 silver badges19 bronze badges 3- what error you have? – Deenadhayalan Manoharan Commented Jun 11, 2015 at 5:26
-
1
container
would be undefined at start. Also, your append will duplicate theid="Description'
– Shaunak D Commented Jun 11, 2015 at 5:28 -
@Deena - No error. Earlier, when I changed the value of
#files
(say 5), 5 textboxes used to get created. Now, the textboxes aren't getting created – Shreyas Tripathy Commented Jun 11, 2015 at 5:29
4 Answers
Reset to default 3There are two ids #files
.
See a working example here: https://jsfiddle/1c3b63f4/ - jQuery will always return an empty string in this assignment: $("#files").val();
.
Put your '$("#files").change(function(){'
code inside 'document.ready'
:-
$(document).ready(function(){
$("#titles").hide();
var container;
// Add & Remove textboxes
$("#files").change(function(){
//Remove all textboxes
$("#titles").show();
$(container).empty();
$(container).remove();
//"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
container = $('<div>', {class: 'controls'});
var option = $("#files").val();
for(i=1;i<=option;i++)
{
// Add a TextBox
$(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
}
$('#titleAdd').after(container); // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
});
});
Try this...
Change id name files to filesdata because you already used id "files" for div
<script src="//code.jquery./jquery-1.11.3.min.js"></script>
<div id="files" class="control-group">
<label class="control-label">No. of files</label>
<div class="controls" >
<select id="filesdata" name="files" class="span3">
<option value="">--Select Option--</option>
<option value="1">1</option>
<option value="3">2</option>
<option value="3">3</option>
</select>
</div>
</div>
<div class="control-group" id="titles">
<label class="control-label">File Titles</label>
<div class="controls" id="titleAdd"></div>
</div>
<script>
$(document).ready(function(){
$("#titles").hide();
var container="";
// Add & Remove textboxes
$("#filesdata").change(function(){
//Remove all textboxes
$("#titles").show();
$(container).empty();
$(container).remove();
//"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
container = $('<div>', {class: 'controls'});
var option = $("#filesdata").val();
for(i=1;i<=option;i++)
{
// Add a TextBox
$(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
}
$('#titleAdd').after(container); // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
});
});
</script>
Demo:https://jsfiddle/jqksjzL1/
you have given same id
"files" for both div
and select
change
<select id="files" name="files" class="span3">
to
<select id="files-select" name="files" class="span3">
and try this js,
$(document).ready(function(){
$("#titles").hide();
});
var container;
// Add & Remove textboxes
$("#files-select").change(function(){
//Remove all textboxes
$("#titles").show();
$(container).empty();
$(container).remove();
//"DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
container = $('<div>', {class: 'controls'});
var option = $("#files-select").val();
for(i=1;i<=option;i++)
{
// Add a TextBox
$(container).append('<input style="display: block;" type=text name="Description[]" class="span3 input-left-top-margins" id="Description' + i +'"' + 'placeholder="File ' + i + ' Title" />');
}
$('#titleAdd').after(container); // ADD THE DIV ELEMENT IN THE RIGHT PLACE.
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744240979a4564704.html
评论列表(0条)