简体   繁体   English

我们如何使用javascript更改html select元素的名称以及仅引用标记的名称?

[英]How do we change the name of a html select element using javascript and only the tags' name to reference to?

HI! 嗨! I have a problem with changing the name of a select element. 我在更改选择元素的名称时遇到问题。 I have about 28 select elements generated on the page. 我在页面上生成了约28个选择元素。 Each of these select elements has been given the name "dropdown". 这些选择元素均已命名为“下拉列表”。 I use this name to calculate the total based on the option selected. 我使用此名称根据所选选项计算总数。

But when i pass this information to a php page, it shows only the last select element. 但是,当我将此信息传递到php页面时,它仅显示最后一个select元素。 To overcome this i need to have all the select tags labelled as "dropdown[]" onsubmit. 为了克服这个问题,我需要在提交时将所有选择标签标记为“ dropdown []”。 This is because i need "dropdown" for javascript to read it and i need "dropdown[]" for php to process it. 这是因为我需要JavaScript的“下拉菜单”才能读取它,而我需要php的“ dropdown []”来处理它。

<select name="dropdown">
<option>
<option>
<option>
</select>

should be changed to : 应该更改为:

<select name="dropdown[]">
    <option>
    <option>
    <option>
    </select>

while validating the form in the end. 最后验证表格。 How do i go about it? 我该怎么办? I dont use ids along with the name, because I think it might make it complex. 我不将id与名称一起使用,因为我认为这可能会使它变得复杂。

I would recommend you to stay with the 'dropdown[]' name, then you can use the getElementsByName function, which will return you an array that you can iterate, of elements with the given name in the document: 我建议您使用'dropdown []'名称,然后可以使用getElementsByName函数,该函数将返回一个可以迭代的数组,其中包含文档中具有给定名称的元素:

var dropdownArray = document.getElementsByName('dropdown[]'),
    i, element, n = dropdownArray.length;

for (i = 0; i < n; i++) {
  element = dropdownArray[i];
  // you can check the value of each element here...
}

Edit: Modifying your code: 编辑:修改您的代码:

function addup(){
  var tot = 0, i, // i declaration was missing
      dropdowns = document.payment.elements['dropdown[]'];

  for(i = 0;i<dropdowns.length;i++) {

    //alert(i);
    var find = dropdowns[i];
    var check = find.options[find.selectedIndex].value;
    //alert(check);

    if(check.substring(0,3)=='pay') {
      // not using eval anymore
      var tot1 = document.payment.elements[check.substring(4)+'_amount'].value; 
      //alert(tot1);
      tot += +tot1; // unary plus operator to convert to number
    }
    document.payment.total_amount.value=tot;
    calcTotal();
  }
}

I think you're approaching this problem wrong. 我认为您正在错误地解决此问题。 You should probably use "id" to uniquely identify elements. 您可能应该使用“ id”来唯一标识元素。 Then you can use one of the many libraries available for free (jQuery, dojo query, ....) to provide you a nicer way to select elements from the page. 然后,您可以使用许多免费的库之一(jQuery,dojo查询...),为您提供一种从页面中选择元素的更好方法。 Either by giving your specific "<select>" elements class names, or just by finding all the "select" elements on the page. 通过给您特定的"<select>"元素类名称,或者仅通过查找页面上的所有“ select”元素即可。

I'm completely in the dark as to why "[]" at the end of the name would make a difference for you. 关于名称末尾的“ []”为什么会对您有所帮助,我完全一无所知。 But I'm not familiar with php. 但是我不熟悉php。

Using this box because i could display the code. 使用此框,因为我可以显示代码。

If i named the element as "dropdown[]" I would be getting an error like in this case:- 如果我将元素命名为“ dropdown []”,则在这种情况下会出现错误:-

function addup(){
var tot=0;
for(i=0;i<(document.payment.dropdown.length);i++)
    {
        //alert(i);
        var find=document.payment.dropdown[][i];
        var check=find.options[find.selectedIndex].value;
        //alert(check);

                    if(check.substring(0,3)=='pay')
                    {
                    var other="document.payment."+check.substring(4)+"_amount.value";
                    var tot1=eval(other);
                    //alert(tot1);
                    tot+=parseInt(tot1);

                    }

                    document.payment.total_amount.value=tot;
                    calcTotal();


    }

}

Pardon the shabby code, but this doesnt seem to work if i name it as "dropdown[]". 请原谅破旧的代码,但是如果我将其命名为“ dropdown []”,这似乎不起作用。 So i need the name to be "dropdown" in the beginning and then it should change to "dropdown[]" onsubmit. 因此,我需要在开头将名称命名为“ dropdown”,然后在提交时将其更改为“ dropdown []”。

I've just found your question. 我刚刚找到了您的问题。

Why to just add a id (example 'itsID') tag to your select and then point it to change its name with: 为什么仅在您的选择中添加一个id(例如'itsID')标记,然后使用以下标记将其指向更改其名称:

var ref = document.getElementById('itsID');
        ref.name = "dropdown[]";

It works for me with ref. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM