我无法弄清楚为什么这不起作用。我试图拉出从json文件循环创建的文本字段的值。
在这个代码中,在最底部我只是做一个简单的点击(function(){alert()},看看我是否可以提取一个值并返回undefined。但是如果我删除'#name'并把它放在'input'中它捕获它,但仅限于几个输入字段中的第一个。
非常感谢任何帮助
JSON
{
"Controls": [{
"Button":[{ "Name":"Button", "x": "1","y": "2","width": "3","height": "4","Transition":"" }],
"Image":[{"x": "5","y": "6","width": "7","height": "8"}],
"TextField":[{"x": "9","y": "10","width": "11","height": "12","Rows":""}]
}]
}
代码(在此之上有soome getJSON内容)
//Slide In Attributes Panel Based on Selected Object
$(document).on('click', '#code li', function () {
var index = $('#code li').index(this);
var selected = $(this).text();
switch (selected) {
case selected:
$('#options').hide();
hidePanels();
$('#temp').remove();
$('#objectAttributes').show("slide", 200);
break;
//If it does work show what variable is being used
default:
alert(selected);
break;
}
//Shows Selected LI Index
$('#codeIndex').text("That was div index #" + index);
//Pull list of Attributes for selected Object
$.getJSON('controls.json', function (data) {
//Build Attributes List
var attributeList = '<div id="temp">';
//Target based on selected object
var target = selected;
attributeList += '<div>' + target + '<div>';
$.each(data.Controls[0][target][0], function (kk, vv) {
attributeList += '<div style="float:right">' + kk + ':' + '<input type="text" id='+ kk + '>' + '</input>' + '</div>';
});
attributeList += '</div></div>';
attributeList += '</div>';
$('#objectAttributes').append(attributeList);
$('#temp').append('<div id="editIndex">'+"Modifying index" + " " +index+'</div>');
$(document).on('click', '#saveAttributes', function () {
var $x = $('#name').val();
alert($x);
})
});
});
答案 0 :(得分:0)
好的,所以在用jsfiddle进行了一次黑客攻击之后,答案变得比我原先想象的要简单得多。自从HTML 4.01类名和ID区分大小写(reference)以来,这意味着您的选择器$('#name')
与JSON Name
不匹配。
因此,一个简单的更改,例如在this simplified jsfiddle中似乎可以按预期工作。希望这有帮助!