基于其他几个线程,我认为我在寻找的是json_encode(),但我并不真正了解如何使用JavaScript函数输出。我想要做的是允许用户根据需要添加其他选择列表,并使用我用来创建初始选择列表的PHP函数填充这些选择选项。这是我目前的JavaScript:需要将PHP数组传递给Javascript函数
<script type="text/javascript">
var assigneeCounter = <?php print checkdata("assignee_count", $formvalues, "1"); ?>;
function addInput(fieldlabel, inputclasses, inputCounter, fieldtype = 'input', selectoptions = false){
window[inputCounter]++;
var cleanlabel = fieldlabel.replace(/[^a-zA-Z 0-9]+/g,'');
var cleanlabel = cleanlabel.replace(/\s+/g, '_').toLowerCase();
var newdiv = document.createElement('div');
newdiv.id = cleanlabel + window[inputCounter];
if (fieldtype == 'input') {
newdiv.innerHTML = '<div class="form-item"><label for="' + cleanlabel + '_' + window[inputCounter] + '">' + fieldlabel + ' #' + window[inputCounter] + '</label><input type="text" name="' + cleanlabel + '_' + window[inputCounter] + '" id="' + cleanlabel + '_' + window[inputCounter] + '" value="" class="' + inputclasses + '"><span class="space"><a href="javascript:removeInput(\'' + cleanlabel + '\',\'' + inputCounter + '\',\'' + window[inputCounter] + '\');">Remove</a></span>';
}
else if (fieldtype == 'select') {
alert(selectoptions);
newdiv.innerHTML = '<div class="form-item"><label for="' + cleanlabel + '_' + window[inputCounter] + '">' + fieldlabel + ' #' + window[inputCounter] + '</label><select name="' + cleanlabel + '_' + window[inputCounter] + '" id="' + cleanlabel + '_' + window[inputCounter] + '" class="' + inputclasses + '">
<span class="space"><a href="javascript:removeInput(\'' + cleanlabel + '\',\'' + inputCounter + '\',\'' + window[inputCounter] + '\');">Remove</a></span>';
}
document.getElementById(cleanlabel + "_additions").appendChild(newdiv);
document.getElementById(cleanlabel + "_count").value = window[inputCounter];
}
function removeInput(fieldlabel, inputCounter, fieldID){
var element = document.getElementById(fieldlabel + fieldID);
document.getElementById(fieldlabel + "_additions").removeChild(element);
window[inputCounter]--;
document.getElementById(fieldlabel + "_count").value = window[inputCounter];
}
</script>
我打电话的功能HTML像这样:
<div id="assignee_additions"></div>
<p><a href="javascript:addInput('Assignee','required-text', 'assigneeCounter', 'select', '<?php print json_encode($assignto); ?>');">Add Another Assignee</a></p>
当我“字段类型”的值设置为输入它的伟大工程,并创建另一个输入如预期的那样。删除功能也完成它的工作。但是我真正需要的是一个选择字段,而不是输入字段,这就是我缺乏JavaScript知识的地方。另外,如果有人似乎认为jQuery在这里是一个更好的选择,我当然也对此敞开心扉,但我不知道比传统的JavaScript更好。
在此先感谢!
那么......问题是什么? :) –
对不起,我猜在那里下了目标!我需要做的是使用我的addInput函数来创建不仅表单输入,而且选择,使用PHP数组来填充选项。 –