我正在做一个内联网表格。该表单允许用户创建“幻灯片显示”(div字段包含输入字段-下拉、文本框和按钮)。用户可以动态地选择添加任意数量的幻灯片。用户还可以更改幻灯片"type“(下拉,它隐藏了一些输入字段)。
我添加div字段的方式是调用一个函数(位于另一个文件中)。该函数只是为每个字段附加具有唯一ID/名称的幻灯片。我还使用jQuery UI使这些幻灯片可排序。除了需要更新某些输入值(或隐藏输入字段)外,一切正常。
我需要解决两件事:
我知道我可以用
$('.type').change(function () { // hide elements });
$( "#main" ).sortable({ update: function() { // update elements}});
但是jQuery似乎不允许我们在附加之后轻松地访问字段。
我已经在网上搜索过可能的解决方案(使用“点击”事件),但什么都找不到。我希望有人能给我指明正确的方向。
谢谢。
发布于 2014-03-23 17:32:21
尝试这个(更新)
function sortInput(el, type, action, val) {
/* `el` : all `input` elements, or, `el#id`, `el.class`, `el[attr]` as `jquery` `object` */
var inputs = $(el);
/* `type` of `$(el)`, i.e.g., `text`, `submit`, `hidden`, etc. */
var type = (type || undefined);
/* if no `newVal` prodided, return `$(this).val() as `default` */
var val = val || $(this).val();
/* `action` to call for `$(el)`, `hide` calls jsquery `.hide()`, `val` calls jquery `.val()` with provided `val` parameter */
$.fn.actions = (action === "hide"
? function() {return this.hide()}
: (action === "val" ? function() {return this.val(val)} : null)
);
/* jquery `.filter()` `$(el)`'s by provided `type` */
return $(inputs).filter(function() {
/* `return` `$(el)`, `filter()`'ed by `type`, call `$.fn.actions()` provided with `action` parameter */
return $(this).attr("type") === type}).actions()
};
/* call `sortInput()`, `.filter()` `type` `submit`, call jquery `.hide()` */
sortInput("input", "submit", "hide");
/* call `sortInput()`, `.filter()` `type` `text`, call jquery `.val()`, with `val` ("newVal") provided parameter */
sortInput("input", "text", "val", "newVal");
试试这个(未经测试)
$(".type").on("change", "input", function (e) {
console.log(e.target); return sortInput("input", "submit", "hide");
});
希望这能有所帮助
https://stackoverflow.com/questions/22598289
复制