使用 php 变量中的值在网站上创建下拉列表?

发布于 2024-11-17 19:58:42 字数 1231 浏览 8 评论 0原文

我正在创建一个表单,用户可以在需要数据时向其表单添加更多字段。

我正在使用一个 javascript 函数,该函数由一个操作按钮触发,该按钮创建一行文本框和一个下拉字段。

然而,在该行中,我需要一个带有“是”和“否”选项的下拉菜单,我已经将它们放在一个名为 $success 的 php 数组变量中,

这就是我必须尝试创建的下拉菜单,但它不起作用。

ta[n]=document.createElement('option');
ta[n].value = <?php echo $success; ?>;
ta[n].name='success'+n;

有人可以帮我吗,谢谢您的时间:)

这是当前代码的大部分

if(inp[c].value=='add') 
    {
       inp[c].onclick=function() 
        {
           ta[n]=document.createElement('input');
           ta[n].setAttribute('rows',1);
           ta[n].setAttribute('cols',20);
           ta[n].name='time'+n;
           document.getElementById('txtara').appendChild(ta[n])
           ta[n]=document.createElement('input');
           ta[n].setAttribute('rows',1);
           ta[n].setAttribute('cols',20);
           ta[n].name='event'+n;
           document.getElementById('txtara').appendChild(ta[n])
           ta[n]=document.createElement('input');
           ta[n].setAttribute('rows',1);
           ta[n].setAttribute('cols',20);
           ta[n].name='supplies'+n;
           document.getElementById('txtara').appendChild(ta[n])
          var sel = document.createElement('select');

I am creating a form that the user can add more fields to their form if they need them for data.

I am using a javascript function which is triggered by a action button that creates a row of textboxes and one pull down field.

However in that row I need a pull down with the options "Yes and No", I already have them in a php array variable called $success

This is what I have to try and create a pull down but its not working.

ta[n]=document.createElement('option');
ta[n].value = <?php echo $success; ?>;
ta[n].name='success'+n;

Could someone help me out thanks for your time :)

This is the bulk of the current code

if(inp[c].value=='add') 
    {
       inp[c].onclick=function() 
        {
           ta[n]=document.createElement('input');
           ta[n].setAttribute('rows',1);
           ta[n].setAttribute('cols',20);
           ta[n].name='time'+n;
           document.getElementById('txtara').appendChild(ta[n])
           ta[n]=document.createElement('input');
           ta[n].setAttribute('rows',1);
           ta[n].setAttribute('cols',20);
           ta[n].name='event'+n;
           document.getElementById('txtara').appendChild(ta[n])
           ta[n]=document.createElement('input');
           ta[n].setAttribute('rows',1);
           ta[n].setAttribute('cols',20);
           ta[n].name='supplies'+n;
           document.getElementById('txtara').appendChild(ta[n])
          var sel = document.createElement('select');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2024-11-24 19:58:42

给你(我认为这就是你想做的):

http://jsfiddle.net/maniator/4brz2 /

var sel = document.createElement('select');

var ta = [];
var n = 0;

ta[n]=document.createElement('option');
ta[n].value = 'YES';
ta[n].name='success'+n;
ta[n].innerHTML = ta[n].value;
n++;
ta[n]=document.createElement('option');
ta[n].value = 'NO';
ta[n].name='success'+n;
ta[n].innerHTML = ta[n].value;
n++;

sel.appendChild(ta[0]);
sel.appendChild(ta[1]);


document.getElementById('here').appendChild(sel);

Here you go (i think this is what you wanted to do):

http://jsfiddle.net/maniator/4brz2/

var sel = document.createElement('select');

var ta = [];
var n = 0;

ta[n]=document.createElement('option');
ta[n].value = 'YES';
ta[n].name='success'+n;
ta[n].innerHTML = ta[n].value;
n++;
ta[n]=document.createElement('option');
ta[n].value = 'NO';
ta[n].name='success'+n;
ta[n].innerHTML = ta[n].value;
n++;

sel.appendChild(ta[0]);
sel.appendChild(ta[1]);


document.getElementById('here').appendChild(sel);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文