这个问题可能看起来像是重复,但我真的找不到类似的问题。事情在这里起作用,但在这里不是动态的:
var counter = 0;
function addInput(divName){
var newdiv = document.createElement('div');
newdiv.innerHTML = "Member " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
但在这里,我给它一个小转折:
var counter = 0;
function addInput(divName){
var newdiv = document.createElement('div');
newdiv.innerHTML = "Member " + (counter + 1) + addmore();
document.getElementById(divName).appendChild(newdiv);
counter++;
}
因此,这里的新函数addmore()
返回由外部PHP代码生成的字段,该代码是在AJAX的帮助下调用的。。
函数addmore();
是这样的:
addmore(){
$jd.ajax({
url: "<?php echo JURI::root(); ?>",
type: "POST",
data: {'option':'com_joomd', 'view':'itempanel', 'task':'loadfields', 'typeid':<?php echo $this->cparams->typeid; ?>, 'catid[]':checked, 'id':<?php echo (int)$this->item->id; ?>, "<?php echo jutility::getToken(); ?>":1, 'abase':1},
beforeSend: function() {
$jd(".poploadingbox").show();
},
complete: function() {
$jd(".poploadingbox").hide();
},
success: function(res) {
$jd('#fieldtable_id').html(res);
},
error: function() {
alert('error');
}
});
}
显然,$jd('#fieldtable_id').html(res);
部分正在做实际的工作,但我不能用它来动态地介绍这里的新字段。
请引导我。
您的函数addmore()没有返回任何内容,因为
- 您的函数中没有
return "value"
- 您正在使用$jd.ajax()进行异步调用
你应该这样做:
var counter = 0;
function addInput(divName){
addmore(divName);
}
和:
function addmore(divName){
$jd.ajax({
url: "<?php echo JURI::root(); ?>",
type: "POST",
data: {'option':'com_joomd', 'view':'itempanel', 'task':'loadfields', 'typeid':<?php echo $this->cparams->typeid; ?>, 'catid[]':checked, 'id':<?php echo (int)$this->item- id; ?>, "<?php echo jutility::getToken(); ?>":1, 'abase':1},
beforeSend: function() {
$jd(".poploadingbox").show();
},
complete: function() {
$jd(".poploadingbox").hide();
},
success: function(res) {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Member " + (counter + 1) + res;
document.getElementById(divName).appendChild(newdiv);
counter++;
},
error: function() {
alert('error');
}
});
}
如果您想从ajax调用返回值。将async设置为false,并使用responseText从ajax调用返回值。然后从addmore函数返回该变量。
从jquery ajax调用返回值。
尝试动态添加/删除输入字段(使用jquery):
<script>$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').live('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
});</script>
<style>
* { font-family:Arial; }
h2 { padding:0 0 5px 5px; }
h2 a { color: #224f99; }
a { color:#999; text-decoration: none; }
a:hover { color:#802727; }
p { padding:0 0 5px 0; }
input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; }
</style>
<div id="ContentWrapper">
<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
<form id="cat" method="POST" action="">
<div id="p_scents">
<p>
<label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
</p>
</div>
新字段的后名称为:p_scnt_1、p_scnt_2等…