可以't使用jQuery设置HTML
Can't set HTML using jQuery
由于某种原因,在删除textbox元素后,脚本没有写出文本。我是错误地使用了.html
还是其他什么问题?
$('.time').click(function () {
var valueOnClick = $(this).html();
$(this).empty();
$(this).append("<input type='text' class='input timebox' />");
$('.timebox').val(valueOnClick);
$('.timebox').focus();
$('.timebox').blur(function () {
var newValue = $(this).val();
var dataToPost = { timeValue: newValue };
$(this).remove('.timebox');
if (valueOnClick != newValue) {
$.ajax({
type: "POST",
url: "Test",
data: dataToPost,
success: function (msg) {
alert(msg);
$(this).html("88");
}
});
} else {
// there is no need to send
// an ajax call if the number
// did not change
alert("else");
$(this).html("88");
}
});
});
好吧,多亏了这些评论,我发现我指的是错误的东西。对我来说,解决方案是更改模糊函数如下:
$('.timebox').blur(function () {
var newValue = $(this).val();
var dataToPost = { timeValue: newValue };
if (valueOnClick != newValue) {
$.ajax({
type: "POST",
url: "Test",
data: dataToPost,
success: function (msg) {
}
});
} else {
// there is no need to send
// an ajax call if the number
// did not change
}
$(this).parent().html("8");
$(this).remove('.timebox');
});
成功处理程序中的$(this)
指的是msg
,而不是$('.timebox')
(或您想将html附加到的任何元素)
$(this) = '.timebox' element but you have removed it already,
$.ajax({
type: "POST",
url: "Test",
data: dataToPost,
success: function (msg) {
alert(msg);
$(this).html("88"); // This = msg
}
and
else {
// there is no need to send
// an ajax call if the number
// did not change
alert("else");
$(this).html("88"); // this = '.timebox' element but you have removed it already,
}
如果您输入一个函数,这个值就会更改。因此,当你在模糊函数处理程序中使用它时,它实际上指向".timebox"
$('.time').click(function () {
var valueOnClick = $(this).html();
var $time=$(this);//If you want to access .time inside the function for blur
//Use $time instead of$(this)
$(this).empty();
$(this).append("<input type='text' class='input timebox' />");
$('.timebox').val(valueOnClick);
$('.timebox').focus();
$('.timebox').blur(function () {
var newValue = $(this).val();
var dataToPost = { timeValue: newValue };
$(this).remove(); //Since $(this) now refers to .timebox
if (valueOnClick != newValue) {
$.ajax({
type: "POST",
url: "Test",
data: dataToPost,
success: function (msg) {
alert(msg);
$(this).html("88");
}
});
} else {
// there is no need to send
// an ajax call if the number
// did not change
alert("else");
$(this).html("88");
}
});
});
相关文章:
- jquery设置为使用参数运行
- Css order属性不't在Jquery设置时更新
- 通过jquery设置最小高度
- 使用jQuery设置HTML5属性值时出现问题
- 如何使用Ajax/jQuery设置cookie
- 通过jQuery设置转换值
- 如何从jQuery设置控制器或全局变量的属性
- javascript/jquery设置var未发生
- jQuery 设置和获取下拉列表和输入的本地存储数据
- 如何使用 jQuery 设置 .selectedIndex
- 使用 jquery 设置 Asp Label 控件的文本值
- 无法从jquery设置标签文本
- 如何使用Jquery设置按钮样式
- jQuery设置属性onclick所有元素,而不是使用内联JS
- 尝试使用jquery设置鼠标按下的时间来更改按钮触发器以控制音量滑块
- 无法使用jquery设置cookie
- 有没有一种快捷方式可以让我用javascript或jQuery设置对象的字段
- 使用Jquery设置页面加载的默认值(我使用的是multiselect插件)
- 单击JQuery设置布尔标志
- textarea根据内容js或jquery设置高度