我正在尝试创建一个动态表,换句话说,一个表,其中每行的列数
- 不一定与其他行相同
- 可以由用户实时更改
在每个添加的列中,都包含一个使用数据库请求填充的下拉框。
我希望能够在页面加载时填充此下拉列表一次,而不是每次添加列时(它用于日历,所以最坏的情况:每行多次 = 31 * x)。
我正在使用 jquery 1.9 和 php 5.3 进行编程。我已经尝试过使用 JSON 和 $.post(),但这会避开太多需要的斜杠和引号,并且由于我的 php 版本 5.3,我无法使用 php 5.4+ 中提供的“DO_NOT_ESCAPE_ANYTHING”常量(是的,我知道那个名字不对,是用心写的)
所以,问题的核心:
如何使用 jquery 和 php 的组合将 HTML 标签放入 javascript 变量中,以便在 jquery 处理的按钮单击时输出。
jsfiddle:http: //jsfiddle.net/pjc3y/3/
代码:
HTML:
<form name="myform" id="myForm" action="#test">
<div>
<table id="persCalTable">
<tr id="DAY_0">
<td>
<input type="text" name="name" size="25" value="Enter your name here!" />
</td>
<td>
<button id="eventAdder">add event</button>
</td>
</tr>
</table>
<input type="submit" value="Submit" id="submitter" />
</div>
</form>
javascript:
function addCellToRow(row, cell) {
row.append(cell);
}
function expandCalendarTable(myObj) {
var DATA = myObj.closest('tr').attr('id').slice(4); //data is the number after DAY_
var selector = '#persCalTable #DAY_' + DATA; //selector is the table and the clicked row id
var cellHiddenField = '<input type="hidden" name="status" value="New" />';
var cellOtherData = "INSERT SELECT TAG HERE";
var cell = cellHiddenField + cellOtherData;
addCellToRow($(selector), cell); // add the cell to the row defined by the selector
eve.preventDefault(); // stop the page refresh(if not in a form, this is not needed, when in a form, this is needed)
//alert('Picked: ' + DATA);
}
$(document).ready(function () {
$("#submitter").click(
function (e) {
e.preventDefault();
alert($('#myForm').serialize());
});
$("[id^=eventAdder]").click(
function (e) {
e.preventDefault();
expandCalendarTable($(this));
});
});