我需要在 javascript 函数中使用 ajax 创建一行,但在找到正确的语法时遇到了很多麻烦。
这是我想要得到的 HTML 最终结果:
<td style="text-align:center">
<input type="image" src="nienteico.png" style="cursor:pointer; width:40px; height:40px" id="5.51" class="ajaxEdit" onclick="cambiastato(5.51)">
</td>
这是需要修改的JS代码:
<td style="+'text-align:center'+"><input type="+'image'+" src="+'nienteico.png'+" style="+'cursor:pointer; width:40px; height:40px'+" id="+'5.51'+" class="+'ajaxEdit'+" onclick="+'cambiastato(5.51)'+"></input></td>
最后这是我使用我之前编写的js获得的HTML:
<td style="text-align:center"><input type="image" src="nienteico.png" style="cursor:pointer;" width:40px;="" height:40px="" id="5.51" class="ajaxEdit" onclick="cambiastato(5.51)"></td>
提前致谢!
最佳答案
不要陷入肮脏的字符串连接,而是选择更干净的代码。
var row = $('<td />', { style: "text-align:center"}).append($('<input />', {
type: 'image',
src: 'nienteico.png',
'class': 'ajaxEdit'
}).css({
id: '5.51',
cursor: 'pointer',
width: '40px',
height: '40px',
}).click(function() { cambiastato(5.51); }));
关于Javascript ajax 语法创建 html 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27342671/