无法使用 JS 创建\编辑选择元素

发布于 2024-12-13 03:24:23 字数 2585 浏览 0 评论 0原文

我正在尝试使用 JS 创建一个选择元素,甚至编辑一个现有的元素,但我似乎遗漏了一些东西。 如果这很重要的话,这是在 Joomla 中完成的。

这是我的代码:

 var option = document.createElement("option");
var select = document.createElement("select");
 select.setAttribute("id", "chooseCat");

for(int i=0;i<LevelNames.Length;i++)
    {
      option.innerHTML = LevelNames[i];
      option.setAttribute("value",LevelIds[i]);
      document.getElementById("cat_chooser").appendChild(option);
      document.getElementById("cat_chooser").options.add(option);
    }

select.onchange=function()
{
  CreateDDL(this.options[this.selectedIndex].value);

}

var test = document.getElementById("cat_chooser");
test.appendChild(select);
document.add(select);
document.appendChild(select);

这是我尝试这样做的所有方法。 cat_chooser 是手动添加到页面的 SELECT。

有什么帮助吗?

编辑: 这是整个代码:

      <script language=\"javascript\" type=\"text/javascript\">


    //definitions
    var LevelNames = new Array();
    var LevelIds = new Array();
    boolean isFirstRun = true;


    //this functions create a Drop Down List 
    function CreateDDL(pid=null){

    //pass arrays for client side, henceforth : var id,var parent_it, var title
    <?php echo "\n".$id."\n".$parent_id."\n".$title."\n\n";?>
    if(pid){

    }
     if(isFirstRun)
        {
    for(int i=0; i < id.length;i++)
            {
    //if category has no parent
       if(parent_id[i] == "1")


            {
                LevelIds.push(id[i]);
                LevelNames.push(title[i]);

                }    
            }
        }
    else{
    for(int i=0; i < id.length;i++)
            {

    //if is a son of our target?
       if(parent_id[i] == pid)
            {
            LevelIds.push(id[i]);
            LevelNames.push(title[i]);

            }    
        }

}
//finished first run
isFirstRun=false;

//create the actuall drop down
//var option = document.createElement("option");
var select = document.createElement("select");
 select.setAttribute("id", "chooseCat");
for(var i=0;i<LevelNames.length;i++)
    {
       var option = new Option(/* Label */ LevelNames[i],
                              /* Value */ LevelIds[i]   );
      select.options.add(option);
    }

    select.onchange=function()
    {
      CreateDDL(this.options[this.selectedIndex].value);

    }
    var test = document.getElementById("cat_chooser");
    test.appendChild(select);
    //document.add(select);
    //document.appendChild(select);
    document.body.appendChild(select);

}
CreateDDL();
</script>

i am trying to create a select element with JS or even edit an existing one yet i seem to be missing something.
this is done in Joomla if this matters.

this is my code:

 var option = document.createElement("option");
var select = document.createElement("select");
 select.setAttribute("id", "chooseCat");

for(int i=0;i<LevelNames.Length;i++)
    {
      option.innerHTML = LevelNames[i];
      option.setAttribute("value",LevelIds[i]);
      document.getElementById("cat_chooser").appendChild(option);
      document.getElementById("cat_chooser").options.add(option);
    }

select.onchange=function()
{
  CreateDDL(this.options[this.selectedIndex].value);

}

var test = document.getElementById("cat_chooser");
test.appendChild(select);
document.add(select);
document.appendChild(select);

this is all the ways i tried doing that.
cat_chooser is a SELECT added manualy to the page.

any help?

EDIT:
this is the whole code :

      <script language=\"javascript\" type=\"text/javascript\">


    //definitions
    var LevelNames = new Array();
    var LevelIds = new Array();
    boolean isFirstRun = true;


    //this functions create a Drop Down List 
    function CreateDDL(pid=null){

    //pass arrays for client side, henceforth : var id,var parent_it, var title
    <?php echo "\n".$id."\n".$parent_id."\n".$title."\n\n";?>
    if(pid){

    }
     if(isFirstRun)
        {
    for(int i=0; i < id.length;i++)
            {
    //if category has no parent
       if(parent_id[i] == "1")


            {
                LevelIds.push(id[i]);
                LevelNames.push(title[i]);

                }    
            }
        }
    else{
    for(int i=0; i < id.length;i++)
            {

    //if is a son of our target?
       if(parent_id[i] == pid)
            {
            LevelIds.push(id[i]);
            LevelNames.push(title[i]);

            }    
        }

}
//finished first run
isFirstRun=false;

//create the actuall drop down
//var option = document.createElement("option");
var select = document.createElement("select");
 select.setAttribute("id", "chooseCat");
for(var i=0;i<LevelNames.length;i++)
    {
       var option = new Option(/* Label */ LevelNames[i],
                              /* Value */ LevelIds[i]   );
      select.options.add(option);
    }

    select.onchange=function()
    {
      CreateDDL(this.options[this.selectedIndex].value);

    }
    var test = document.getElementById("cat_chooser");
    test.appendChild(select);
    //document.add(select);
    //document.appendChild(select);
    document.body.appendChild(select);

}
CreateDDL();
</script>

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

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

发布评论

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

评论(2

不疑不惑不回忆 2024-12-20 03:24:23
  • JavaScript不是Java。您不能使用 intboolean 来声明变量。相反,请使用 var
  • JavaScript不是PHP。您无法使用function createDDL(pid=null) 定义默认值
  • .add 方法仅在HTMLSelectElement.options 对象中定义。
  • .appendChild 应该用在 document.body 上,而不是 document,因为你想将 elemetns 添加到正文,而不是文档。

工作代码,前提是 返回有效的 JavaScript 对象。

<script language="javascript" type="text/javascript"> //No backslashes..
//definitions
var LevelNames = new Array();
var LevelIds = new Array();
var isFirstRun = true;

//this functions create a Drop Down List 
function CreateDDL(pid) {
    if(typeof pid == "undefined") pid = null; //Default value
    //pass arrays for client side, henceforth : var id,var parent_it, var title
    <?php echo "\n".$id."\n".$parent_id."\n".$title."\n\n"; ?>
    if (pid) {

    }
    if (isFirstRun) {
        for (var i = 0; i < id.length; i++) {
            //if category has no parent
            if (parent_id[i] == "1")

            {
                LevelIds.push(id[i]);
                LevelNames.push(title[i]);

            }
        }
    } else {
        for (var i = 0; i < id.length; i++) {

            //if is a son of our target?
            if (parent_id[i] == pid) {
                LevelIds.push(id[i]);
                LevelNames.push(title[i]);

            }
        }

    }
    //finished first run
    isFirstRun = false;

    //create the actuall drop down
    //var option = document.createElement("option");
    var select = document.createElement("select");
    select.setAttribute("id", "chooseCat");
    for (var i = 0; i < LevelNames.length; i++) {
        var option = new Option(/* Label */ LevelNames[i],
                                /* Value */ LevelIds[i]);
        select.options.add(option);
    }

    select.onchange = function () {
        CreateDDL(this.options[this.selectedIndex].value);

    }
    var test = document.getElementById("cat_chooser");
    test.appendChild(select);
    //document.add(select);
    //document.appendChild(select);
    document.body.appendChild(select);

}
CreateDDL();
</script>
  • JavaScript is not Java. You cannot use int or boolean to declare variables. Instead, use var.
  • JavaScript is not PHP. You cannot define a default value using function createDDL(pid=null)
  • The .add method is only defined at the HTMLSelectElement.options object.
  • .appendChild should be used on document.body, not document, because you want to add elemetns to the body, rather than the document.

Working code, provided that <?php .. ?> returns valid JavaScript objects.

<script language="javascript" type="text/javascript"> //No backslashes..
//definitions
var LevelNames = new Array();
var LevelIds = new Array();
var isFirstRun = true;

//this functions create a Drop Down List 
function CreateDDL(pid) {
    if(typeof pid == "undefined") pid = null; //Default value
    //pass arrays for client side, henceforth : var id,var parent_it, var title
    <?php echo "\n".$id."\n".$parent_id."\n".$title."\n\n"; ?>
    if (pid) {

    }
    if (isFirstRun) {
        for (var i = 0; i < id.length; i++) {
            //if category has no parent
            if (parent_id[i] == "1")

            {
                LevelIds.push(id[i]);
                LevelNames.push(title[i]);

            }
        }
    } else {
        for (var i = 0; i < id.length; i++) {

            //if is a son of our target?
            if (parent_id[i] == pid) {
                LevelIds.push(id[i]);
                LevelNames.push(title[i]);

            }
        }

    }
    //finished first run
    isFirstRun = false;

    //create the actuall drop down
    //var option = document.createElement("option");
    var select = document.createElement("select");
    select.setAttribute("id", "chooseCat");
    for (var i = 0; i < LevelNames.length; i++) {
        var option = new Option(/* Label */ LevelNames[i],
                                /* Value */ LevelIds[i]);
        select.options.add(option);
    }

    select.onchange = function () {
        CreateDDL(this.options[this.selectedIndex].value);

    }
    var test = document.getElementById("cat_chooser");
    test.appendChild(select);
    //document.add(select);
    //document.appendChild(select);
    document.body.appendChild(select);

}
CreateDDL();
</script>
我要还你自由 2024-12-20 03:24:23

您需要创建一个新元素并将其附加到每次迭代中。目前,整个 for 循环将数据附加到同一选项。

此外,在 for 循环语句中,您可以对 i 变量进行类型转换,而这在 JavaScript 中是无法做到的。

You need to create a new element and append it in each iteration. Currently, the entire for loop append data to the same option.

Also, in the for loop statement, you typecast the i variable, which you can't do in JavaScript.

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