Javascript 基于锚定值的动态表单输入
我有以下打开单个模式表单的代码段:Javascript 基于锚定值的动态表单输入,javascript,html,dynamic,anchor,Javascript,Html,Dynamic,Anchor,我有以下打开单个模式表单的代码段: <a class="modalbox" id="foo" href="#inline">Request a Quote</a> <a class="modalbox" id="bar" href="#inline">Request a Quote</a> ...and so on... 我建议使用纯JavaScript,类似于: function addTextToInput(from, to, prefix,
<a class="modalbox" id="foo" href="#inline">Request a Quote</a>
<a class="modalbox" id="bar" href="#inline">Request a Quote</a>
...and so on...
我建议使用纯JavaScript,类似于:
function addTextToInput(from, to, prefix, e) {
e = e || window.event;
if (!from || !to) {
return false;
}
else {
from = from.nodeType == 1 ? from : document.getElementById(from);
to = to.nodeType == 1 ? to : document.getElementById(to);
var text = from.id;
to.value = prefix ? prefix + ' ' + text : text;
}
}
var as = document.querySelectorAll('a.modalbox');
for (var i = 0, len = as.length; i<len; i++) {
as[i].onclick = function(e) {
addTextToInput(this, 'sub', 'I am interested in', e);
};
}
.Perfect-您发布的JQuery解决方案立即生效。非常感谢。(另外,我有点沾沾自喜的是,简单的JS版本也能工作!=D)
$('a').click(function(e) {
$('#product').val($(this).attr('id'));
function addTextToInput(from, to, prefix, e) {
e = e || window.event;
if (!from || !to) {
return false;
}
else {
from = from.nodeType == 1 ? from : document.getElementById(from);
to = to.nodeType == 1 ? to : document.getElementById(to);
var text = from.id;
to.value = prefix ? prefix + ' ' + text : text;
}
}
var as = document.querySelectorAll('a.modalbox');
for (var i = 0, len = as.length; i<len; i++) {
as[i].onclick = function(e) {
addTextToInput(this, 'sub', 'I am interested in', e);
};
}
$('a.modalbox').click(function(e){
e.preventDefault();
$('#sub').val('I am interested in ' + this.id);
});