$(document).ready(function()
{
var oldText, newText;
$(".editable").hover(
function()
{
$(this).addClass("editHover");
},
function()
{
$(this).removeClass("editHover");
}
);
$(".editable").bind("dblclick", replaceHTML);
$(".btnSave").live("click",
function()
{
newText = $(this).siblings("form")
.children(".editBox")
.val().replace(/"/g, """);
currentId = $(this).parent().attr("id");
$.ajax({
type: "POST",
url: "./musa_date_edit_ajax.php",
data: "vidpubdate="+newText+"&id=" + currentId,
success: function(msg){
//alert("test ok");
}
});
$(this).parent()
.html(newText)
.removeClass("noPad")
.bind("dblclick", replaceHTML);
}
);
$(".btnDiscard").live("click",
function()
{
$(this).parent()
.html(oldText)
.removeClass("noPad")
.bind("dblclick", replaceHTML);
}
);
function replaceHTML()
{
oldText = $(this).html()
.replace(/"/g, """);
$(this).addClass("noPad")
.html("")
.html("<form><input type=\"text\" class=\"editBox\" value=\"" + oldText + "\" /> </form><button type=\"button\" class=\"btnSave\" >planifier</button> <!-- <button type=\"button\" class=\"btnDiscard\" >Annuler</button> -->")
.unbind('dblclick', replaceHTML);
}
}
);
IE / Chrome说:参考以下代码:
newText = $(this).siblings("form")
.children(".editBox")
.val().replace(/"/g, """);
奇怪的是,它在Firefox上运行得非常好 我想它与$ this有关,似乎未定义。但由于我不是JS / Jquery大师,我不知道如何在Chrome和IE上修复它。
附录:
这里或多或少是HTML的样子:
<form>
blabla
<table>
<tr>
<td>blabla</td>
<td class="editable" id="<?php echo $data['id']; ?>" ><?php echo $data['date'];?></td>
</tr>
</table>
</form>
更新
<form>
<table>
<td>some useless stuff</td>
<td class="editable noPad" id="1732">
<form>
<input class="editBox" value="22/05/13" type="text">
</form>
<button type="button" class="btnSave">planifier</button>
<!-- <button type="button" class="btnDiscard">Annuler</button> -->
</td>
</table>
</form>
答案 0 :(得分:1)
可能只是缩短的html片段中的拼写错误,但您在html中使用“可编辑”类,并在javascript中使用“编辑框”。
$('.editbox')
返回空,因此.val()
返回undefined,因此replace()
会导致异常
要弄清楚父/兄弟的事情是否正确,你需要在你的html中显示更多细节
编辑:怎么样:
$(this).parent().find('.editBox').val().replace(...
答案 1 :(得分:0)
试试这个:
$(this).closest("td").find(".editBox").val().replace(/"/g, """);
另外,一个建议。您可以将此当前代码最小化:
$(".editable").hover(
function () {
$(this).addClass("editHover");
},
function () {
$(this).removeClass("editHover");
});
通过这样做:
$(".editable").hover(
function () {
$(this).toggleClass("editHover");
});