【发布时间】:2015-09-09 00:30:04
【问题描述】:
请看一下这段代码。我只想刷新表单的一部分而不是整个页面。当前代码正在刷新整个页面。由于所有条目都在数据库中,因此无需查找数据库错误
<script>
$(document).ready(function() {
$('#refbutton').click(function() {
var val = $('#refinfo').val();
$.ajax({
type: 'POST',
url: 'selectbox.php',
data: 'val=' + val,
success: function(html) {
alert(html);
window.location.reload();
}
});
});
});
</script>
HTML 代码
<form>
<table>
<tr>
<td >Name</td>
<td ><input name="name" type="text" /></td>
</tr>
<tr>
<td>Address</td>
<td><textarea name="address" ></textarea></td>
</tr>
<div id="mydiv">
<tr>
<td>Reference</td>
<td>
<select data-placeholder="Choose a reference..." class="chosen-select" name ="reference1" style="width:350px;" tabindex="2">
<option value="default">Please Select</option>
<?php
$sql="select * from reference ";
//echo $sql;
$tbl=mysql_query($sql);
while($row=mysql_fetch_array($tbl))
{
?>
<option value="<?php echo $row['reference'];?>"><?php echo $row['reference'];?></option>
<?php }?>
</select>
</td>
<td>
<input name="reference2" type="text" id="refinfo" ></input>
</td>
<td>
<input name="Button_ref" id="refbutton" type="button" value="Add Reference"></td>
</tr>
</div>
</table>
</form>
【问题讨论】:
-
删除
window.location.reload();并使用$('div').html(html) -
@ShaunakD 这样我的条目就不会被添加到选择框中
-
将值附加到选择框而不是重新加载页面。
-
@lshettyl 我想将 reference2 中的值添加到 db 中,然后在选择框中从 db 中获取值
-
兄弟我想你应该看看JavaScript中的同步和异步是什么?截至目前。
标签: javascript jquery html ajax dom