我已经创建了一个模态窗口来更新从数据库中获取的记录,现在我想要相同的模态窗口来添加新记录,我不想为添加目的(代码可重用性)编写相同的表单代码,但是我如何动态地将<legend>Update record</legend>
更改为<legend>Add record</legend>
。
<div id="modal">
<div id="update">
<span class="closeBttn">X</span>
<form action="" method="post">
<fieldset>
<legend>Update Record</legend>
<p>
<label for="name">Name: </label><br />
<input type="text" id="name" name="name" />
</p>
<p>
<label for="subject">Subject: </label><br />
<input type="text" id="subject" name="subject" />
</p>
<p>
<label for="theory">Theory: </label><br />
<input type="text" id="theory" name="theory" />
</p>
<p>
<label for="practical">Practical: </label><br />
<input type="text" id="practical" name="practical" />
</p>
<p>
<input type="hidden" name="id" id="id" />
<input type="hidden" name="action" value="update" />
<input type="submit" name="submit" value="Update" />
</p>
</fieldset>
</form>
</div>
</div>
try this:
$(function(){
var id = $('#id').val();
if(id > 0)
{
$("#modal legend").text("Update Record");
}else{
$("#modal legend").text("Update Record");
}
});
你也可以简单地使用PHP…参见下面的代码
<?php
if(!empty($_REQUEST['id']))//fetching from url
{
echo '<legend>Update Record</legend>';
}
else
{
echo '<legend>Add Record</legend>';
}
?>