我绞尽脑汁经历它,查看示例并且不明白为什么firebug会抛出"e.nodeName is undefined"错误.
它可能是一个愚蠢的小支架不合适或需要第二双眼睛看到的东西..
我只是为一些输入做了一个简单的小ajax帖子,但这是我的第一篇文章,我关于由于我到目前为止遇到了多少错误而把头发拉了下来...
http://jsfiddle.net/JohnnyDoe/aQYra/
我的剧本
<script type="text/javascript">
$(document).ready(function () {
$('.hexen').after('<div class="ui-state-default ui-corner-all ui-icon-disk ui-icon saveButton" onClick="save();" title="Save" style="float:left; height:20px;" onclick="save()"></div><br />') // ui icon
.keypress(function () {
$(this).next('.saveButton').show(); //appends ui icon
});
$('.saveButton').hide().click(function () {
$(this).hide(); // removes ui icon on click
});
$('.ui-state-default').hover(
function () {
$(this).addClass('ui-state-hover');
}, function () {
$(this).removeClass('ui-state-hover');
} //ui icon hover
);
});
function save(value) {
$('.hexen').each(function () {
var id = null;
});
if ($(this).val() !== '') {
id = $(this).attr('id');
}
}
$.ajax({
type: "POST",
url: "Default.aspx",
data: "{Id: " + $(".hexen").attr('id') + ", Value: " + $(".hexen").val() + "}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
我的HTML
<div id="unvolitive">
<input type="text" class="hexen" id="Text1"/>
<input type="text" class="hexen" id="Text2"/>
<input type="text" class="hexen" id="Text3"/>
<input type="text" class="hexen" id="Text4"/>
<input type="text" class="hexen" id="Text5"/>
</div>
Run Code Online (Sandbox Code Playgroud)
提前致谢
我认为,从一个简短的小提琴测试中,问题来自混合普通的jQuery事件处理程序和onclick
你在标记字符串(你的save
函数)中定义的处理程序.
我不确定你要做什么save
; 这个each
电话什么都不做:
$('.hexen').each(function () {
var id = null; // all this does is set a local variable
});
Run Code Online (Sandbox Code Playgroud)
但更重要的是,你的方式已经与此设置onclick
,this
将是不确定的,所以下面一行将无法正常工作:
if ($(this).val() !== '') {
Run Code Online (Sandbox Code Playgroud)
在这种情况下,this
指的是window
对象,所以我认为这就是你得到错误的原因.要修复它,您应该在jQuery分配的单击处理程序中处理save函数:
$('.saveButton').hide()
.click(function () {
$(this).hide(); // removes ui icon on click
var id;
// get the associated input
var $input = $(this).prev('.hexen');
if ($input.val() !== '') {
id = $input.attr('id');
}
console.log(id);
// presumably, do something with id now...
});
Run Code Online (Sandbox Code Playgroud)
看到一个工作小提琴.
错误发生在上下文之外的save()
用途this
..(它指的是当前的窗口)
它可能应该是
function save(value) {
$('.hexen').each(function() {
var id = null;
if ($(this).val() !== '') {
id = $(this).attr('id');
}
});
}
Run Code Online (Sandbox Code Playgroud)
我在处理程序中移动了if
部件each
..
归档时间: |
|
查看次数: |
13216 次 |
最近记录: |