-1
我想追加選擇框值到jQuery點擊。換句話說,當單擊添加按鈕時,它應該顯示下一行,其中有一個選擇框(即來自數據庫)和一個文本框以輸入百分比。使用jQuery附加選擇框使用AJAX,PHP和MySQL
單擊添加按鈕時的下面的代碼帶來了一個新的選擇框和一個文本框,但是選擇框的值爲空。
我知道在調用這一行時我的語法是錯誤的。
$('#addr'+i).html("<td>"+ (i+1) +"</td><td><select class='form-control input-md'>"+ (selectClone) +"</select></td><td><input name='percent"+i+"' type='text' placeholder='Percent' class='form-control input-md'/></td>");
全碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Author Page</title>
<link href="bootstrap.css" rel="stylesheet">
<script src="jquery-1.11.0.min.js"></script>
<script src="bootstrap.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var i=1;
$("#add_row").click(function(){
var selectClone = $('select:first').clone(true);
// i have problem with the below line. I want to append the select box value here
$('#addr'+i).html("<td>"+ (i+1) +"</td><td><select class='form-control input-md'>"+ (selectClone) +"</select></td><td><input name='percent"+i+"' type='text' placeholder='Percent' class='form-control input-md'/></td>");
$('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
i++;
});
});
</script>
</head>
<body>
<?php
mysql_connect("localhost", "root", "12345") or die(mysql_error());
mysql_select_db("book_author") or die(mysql_error());
$rsAuth=mysql_query("select author_id,author_name from authors");
?>
<form method="post" action="process_author_values.php">
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
#
</th>
<th class="text-center">
Book Name
</th>
<th class="text-center">
Percentage
</th>
</tr>
</thead>
<tbody>
<tr id='addr0'>
<td>
1
</td>
<td id="abc">
<select class="form-control" id="abc" name="name0[]">
<option value="0">Select an Author</option>
<?php while($row2=mysql_fetch_array($rsAuth)){?>
<option value="<?php echo $row2['author_id'];?>"><?php echo $row2['author_name']; ?> </option>
<?php }?>
</select>
</td>
<td><input type="text" name='percent0' placeholder='Percent' class="form-control"/></td>
</tr>
<tr id='addr1'></tr>
</tbody>
</table>
</div>
</div>
<a id="add_row" class="btn btn-default pull-left">Add Author</a>
</div>
<input type="submit" value="submit" />
</form>
</body>
</html>
是的..這工作。萬分感謝。順便說一句 - 誰低估了我的問題;( – user3350885