我有一點javascript,我想用它來允許用戶輸入無限選項和無限圖像URLS。按鈕使JavaScript無意中重新加載頁面
我已經做了jsFiddle
下面是HTML:
<div class="container-fluid body">
<div class="admin-box">
<form action="http://localhost/MyZone/public_html/index.php/create_option/2" class="form-horizontal" method="post" accept-charset="utf-8"><div style="display:none">
<input type="hidden" name="ci_csrf_token" value="001b4b051689692edec29d09b9837f3a">
</div> <fieldset>
<legend>Option Title</legend>
<div class="control-group">
<label class="control-label" for="Option title">title</label>
<div class="controls">
<input type="text" name="name" id="name" class="span6">
</div>
</div>
<div class="admin-box" id="option_information">
<fieldset>
<legend>Option information</legend>
<div class="control-group">
<label class="control-label" for="value">Description</label>
<div class="controls">
<input type="text" name="value" id="value" class="span6">
</div>
</div>
<div class="control-group">
<label class="control-label" for="cost_including_vat">Cost including VAT</label>
<div class="controls">
<input type="text" name="cost_including_vat" id="cost_including_vat" class="span6">
</div>
</div>
<div class="control-group">
<label class="control-label" for="vat_of_cost">VAT of cost</label>
<div class="controls">
<input type="text" name="vat_of_cost" id="vat_of_cost" class="span6">
</div>
</div>
<div class="control-group">
<label class="control-label" for="sale_price_including_vat">Sale price including vat</label>
<div class="controls">
<input type="text" name="sale_price_including_vat" id="sale_price_including_vat" class="span6">
</div>
</div>
<div class="control-group">
<label class="control-label" for="vat_of_sale_price">VAT of sale price</label>
<div class="controls">
<input type="text" name="vat_of_sale_price" id="vat_of_sale_price" class="span6">
</div>
</div>
<div id="image_addition">
</div>
<div class="control-group">
<button class="controls" id="add_image" value="Add image">Add Image</button>
</div>
</fieldset>
</div>
<div class="control-group">
<button class="controls" id="add_option" value="Add option">Add option</button>
</div>
<div class="form-actions">
<br>
<input type="submit" name="save" class="btn btn-primary" value="Create option">
or <a href="http://localhost/MyZone/public_html/index.php" class="btn btn-warning">Cancel</a> </div>
</fieldset>
</form>
這裏是JavaScript
var counter_image = 1;
$('#add_image').click(function()
{
var newrow = '<div class="control-group">'+
'<label class="control-label" for="image_name'+counter_image+'">Image '+counter_image+' name</label>'+
'<div class="controls">' +
'<input type="text" name="image_name'+counter_image+'" id="image_name'+counter_image+'" class="span6" />'+
'</div>' +
'</div>' +
'<div class="control-group">' +
'<label class="control-label" for="image_description'+counter_image+'">Image '+counter_image+' message</label>' +
'<div class="controls">' +
'<input type="text" name="image_description'+counter_image+'" id="image_description'+counter_image+'" class="span6" />' +
'</div>' +
'</div>' +
'<div class="control-group">' +
'<label class="control-label" for="image_url'+counter_image+'">Image '+counter_image+' URL</label>' +
'<div class="controls">' +
'<input type="url" name="image_url'+counter_image+'" id="image_url'+counter_image+'" class="span6" />' +
'</div>' +
'</div>';
$('#image_addition').append(newrow);
counter_image++;
return false;
});
var counter_option = 1;
$('#add_option').click(function()
{
var newrow = '<fieldset>' +
'<legend>Option information</legend>'+
'<div class="control-group">'+
'<label class="control-label" for="value">Description</label>'+
'<div class="controls">'+
'<input type="text" name="value" id="value" class="span6" />'+
'</div>'+
'</div>'+
'<div class="control-group">'+
'<label class="control-label" for="cost_including_vat">Cost including VAT</label>'+
'<div class="controls">'+
'<input type="text" name="cost_including_vat" id="cost_including_vat" class="span6" />'+
'</div>'+
'</div>'+
'<div class="control-group">'+
'<label class="control-label" for="vat_of_cost">VAT of cost</label>'+
'<div class="controls">'+
'<input type="text" name="vat_of_cost" id="vat_of_cost" class="span6" />'+
'</div>'+
'</div>'+
'<div class="control-group">'+
'<label class="control-label" for="sale_price_including_vat">Sale price including vat</label>'+
'<div class="controls">'+
'<input type="text" name="sale_price_including_vat" id="sale_price_including_vat" class="span6" />'+
'</div>'+
'</div>'+
'<div class="control-group">'+
'<label class="control-label" for="vat_of_sale_price">VAT of sale price</label>'+
'<div class="controls">'+
'<input type="text" name="vat_of_sale_price" id="vat_of_sale_price" class="span6" />'+
'</div>'+
'</div>'+
'<div id="image_addition">'+
'</div>'+
'<div class="control-group">'+
'<button class="controls" id="add_option" value="Add image">Add Image</button>'+
'</div>'+
'</fieldset>';
$('#option_information').append(newrow);
counter_option++;
return false;
});
所以基本上我可以繼續添加選項天晴及我可以在任何時候添加無限圖像e先前的選項(生成的)當我按下'添加圖像'它只是重新加載頁面使我失去了生成的所有東西?
這個想法是,我想讓某人爲每個選項輸入儘可能多的選項和圖像,因爲他們想要添加,然後最終將它們添加到數據庫中。
感謝
記住一個答案是否正確? –