如何使用javascript/jquery和javascript/jquery在表中添加更多行?
我正在为教师更换列表生成器 我的表中有如下列,例如:如何使用javascript/jquery和javascript/jquery在表中添加更多行?,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我正在为教师更换列表生成器 我的表中有如下列,例如: <div id="formReplacements"> <table id="myTable"> <tr> <td>Day</td><td>Class</td><td>Lesson number</td><td&
<div id="formReplacements">
<table id="myTable">
<tr>
<td>Day</td><td>Class</td><td>Lesson number</td><td>Lesson</td><td>Teacher</td><td>Room</td>
</tr>
<tr>
<td>
<select id="day" name="day">
<?php
if (date(N) >= 6)
{
echo '<option value="'.dateV('j f Y',strtotime("next Monday")).'">Monday ('.dateV('j f Y',strtotime("next monday")).')</option>';
echo '<option value="'.dateV('j f Y',strtotime("next Tuesday")).'">Tuesday ('.dateV('j f Y',strtotime("next tuesday")).')</option>';
echo '<option value="'.dateV('j f Y',strtotime("next Wednesday")).'">Wednesday ('.dateV('j f Y',strtotime("next wednesday")).')</option>';
}
//some more code in php here, irrelevant to the question...
?>
<option value="another" name="another" id="another">Another...</option>
</select>
<script type="text/javascript">
var selectmenu=document.getElementById("day")
selectmenu.onchange=function(){ //run some code when "onchange" event fires
var chosenoption=this.options[this.selectedIndex]
if (chosenoption.value=="another"){
var test = window.prompt("Write the date","");
document.getElementById('another').value = test;
document.getElementById('another').text = test;
}
}
</script>
</td>
<td>
//etc...
DayClassLessonNumbersOnteacherRoom
我认为你不能在javascript中像那样使用php。echo
?这不是JavaScript…JavaScript中的PHP工作得很好。然而,如果它没有被广泛使用,那么它是什么呢?php和javascript在完全不同的环境中运行。服务器上的php和浏览器中的js。您似乎没有考虑将此代码保存在*.php文件中,并在WebServ上运行。我不想在新行中动态添加php代码。
<HEAD>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var counter=1;
$("#add_replacement").click(function () {
counter++;
myTable = document.getElementById('myTable');
myTable.innerHTML = myTable.innerHTML+'<tr><td><select id="day'+counter+'" name="day'+counter+'"><?php
if (date(N) >= 6)
{
echo '<option value="'.dateV('j f Y',strtotime('next Monday')).'">Monday ('.dateV('j f Y',strtotime("next monday")).')</option>';
echo '<option value="'.dateV('j f Y',strtotime("next Tuesday")).'">Tuesday ('.dateV('j f Y',strtotime("next tuesday")).')</option>';
echo '<option value="'.dateV('j f Y',strtotime("next Wednesday")).'">Wednesday ('.dateV('j f Y',strtotime("next wednesday")).')</option>';
}
//...
?><option value="another" name="another" id="another">Another...</option></select><script type="text/javascript">var selectmenu=document.getElementById("day'+counter+'");selectmenu.onchange=function(){ var chosenoption=this.options[this.selectedIndex]; if (chosenoption.value=="another"){var test = window.prompt("Write the date","");document.getElementById("another").value = test;document.getElementById("another").text = test;}}<\/script>';
myTable.innerHTML = myTable.innerHTML+'</td><td><select name="classes"><optgroup label="Classes I">';
document.getElementById('number_of_replacements').value = counter;
});
});
</script>
</HEAD>
<input type="button" id="add_replacement" value="Add new replacement..." />