2012-09-20 86 views
0

基于其他几个线程,我认为我在寻找的是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更好。

在此先感谢!

+1

那么......问题是什么? :) –

+0

对不起,我猜在那里下了目标!我需要做的是使用我的addInput函数来创建不仅表单输入,而且选择,使用PHP数组来填充选项。 –

回答

1

您可以在不同的php文件中创建您的JSON。然后,从javascript函数中,可以使用AJAX调用来检索生成的JSON

我相信你想为<option>标签设置json_encode的值和密钥吧?所以,成功拨打AJAX后,请解码您的JSON,并为每个值创建一个新选项。 jQuery中

粗糙的代码可以是这样的:

$.ajax({ 
    url: 'giveMeJSON.php', 
    dataType: 'json', 
    success: function(data){ 
     $.each(data.options, function(index){ 
      $('<option>').val($(this).value).text($(this).text).appendTo($('#select_id')); 
     } 
    } 
}) 

阅读$.ajaxAJAX

+0

嗯 - 我已经遇到过这种ajax语法,但我不知道如何修改我已经得到的(或完全废除)以使其能够同时创建常规输入字段以及选择。 –

+0

我发现了另一个链接(http://www.daveismyname.com/tp-dynamic-form-elements-with-jquery),与您的组合让我感觉到我走向了正确的轨道。 –