页面上第二个表单所需的唯一 id 属性
Unique id attributes needed for second form on page
我有一个脚本,可以将日期从日期选择器中分隔为日,月和年。当页面上有一个表单时,它工作正常,但当有第二个表单时则不然(可能是由于 id 相同)。如何向我的 id 和名称添加值,使它们成为唯一值?
var now = new Date();
var date = jQuery('.form-date');
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
date.val(today);
jQuery('form').on('submit', function() {
var newVal = date.val().split('-'),
dateParts = {
year: parseInt(newVal[0], 10),
month: parseInt(newVal[1], 10),
day: parseInt(newVal[2], 10)
};
jQuery('.anchor').append(jQuery.map(dateParts, function (index, key) {
var name = 'date_' + key;
return jQuery('<input>', {
type: 'hidden',
name: name,
id: name,
value: dateParts[key]
});
}));
});
需要声明一个确定当前形式的变量:
(function ($) {
var now = new Date();
var date = $('.form-date');
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear() + "-" + (month) + "-" + (day);
date.val(today);
var form = $('form.reservation');
form.on('submit', function () {
var currentForm = $(this);
var newVal = currentForm.find('.form-date').val().split('-'),
dateParts = {
year: parseInt(newVal[0], 10),
month: parseInt(newVal[1], 10),
day: parseInt(newVal[2], 10)
};
currentForm.find('.anchor').append($.map(dateParts, function (index, key) {
console.log(['date', form.index(currentForm), key].join('_'));
return $('<input>', {
type: 'hidden',
name: ['date', key].join('_'),
id: ['date', form.index(currentForm), key].join('_'),
value: dateParts[key]
});
}));
});
})(jQuery);
相关文章:
- 正在为循环创建唯一id
- 在JavaScript中创建全局唯一ID
- 为dropdownlistfor(MVC)中的每个元素设置唯一id
- 向具有唯一Id的表添加/删除行
- 基于唯一ID合并geojson
- 如何为使用 jQuery 动态生成的按钮提供唯一 ID
- 类似系统:循环形成唯一id,得到这些id'使用jQuery/Javascript单独发布
- Knockout.js中使用的唯一ID;对于“;单选按钮组中的标签属性
- 在所有页面中搜索<身体>具有唯一ID
- 如何从多个唯一id中获取值
- 为客户端和服务器上的对象生成唯一 ID
- 如何使用jQuery创建具有唯一ID的可重复表
- 在 Firebase 中通过对象的唯一 id 查找对象
- 如何将唯一 ID 添加到 Javascript 中动态创建的输入字段
- 页面上第二个表单所需的唯一 id 属性
- 克隆并附加唯一 ID 后找不到 DOM 元素
- 如何在 append() 上添加具有唯一 id 的编辑按钮
- Chrome 标签页的永久唯一 ID,在浏览器会话之间持续存在
- 跨多个客户端和服务器生成真正的全局唯一 ID
- 如何通过 php 中的唯一 ID 删除 Json