0

我是 javascript 的新手,我正在努力使用以下代码,该代码将以注册多个候选人的形式出现。

它为每个候选人创建 2 个相关的选择框(国家和地区)。

单击“添加候选者”按钮一次可以使相关框正常工作,但再次单击该按钮会使其停止工作。当有多个候选人时,从表单中访问选定的值也是不可能的,因为它们会相互覆盖。

我尝试使用计数变量将选择名称创建为数组,每次调用 ff 函数时我都会递增该变量,但我无法让它工作。

所有帮助将不胜感激!

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<title>Select Populate Test</title>

<script>
var UnitedStates = new Array();
UnitedStates[0] = "Texas";
UnitedStates[1] = "California";
UnitedStates[2] = "Arizona";
UnitedStates[3] = "Nevada";
UnitedStates[4] = "Florida";

var UnitedKingdom = new Array();
UnitedKingdom[0] = "Surrey";
UnitedKingdom[1] = "Kent";
UnitedKingdom[2] = "Dorset";
UnitedKingdom[3] = "Hampshire";

function populateDropdown(arry)
{
    document.myForm.stateSelect.options.length = 0;

    for (var i = 0; i < arry.length; i++)
    {
        document.myForm.stateSelect.options[i] = new Option(arry[i], arry[i]);
    }
}

function updateDropdown(str)
{
    var stateArray
    var selectedCountry;
    var countryDropdown = document.myForm.countrySelect;

    for (var i = 0; i < countryDropdown.options.length; i++)
    {
        if (countryDropdown.options[i].selected)
        {
            selectedCountry = countryDropdown.options[i].value;
        }
    }

    if (selectedCountry == 1)
    {
        stateArray = UnitedStates;
        populateDropdown(stateArray);
    }

    if (selectedCountry == 2)
    {
            stateArray = UnitedKingdom;
        populateDropdown(stateArray);
    }
}

counter = 0;
function ff()
{
        counter++;
        var box = document.getElementById("details"+counter);

        var cselectBox = document.createElement("Select");
        cselectBox.name="countrySelect";
        cselectBox.onchange = function()
        {
            updateDropdown();
        }

        var option1 = document.createElement("OPTION"); 
        option1.text="United States";  
        option1.value=1;  
        cselectBox.options.add(option1);

        var option2 = document.createElement("OPTION"); 
        option2.text="United Kingdom";  
        option2.value=2;  
        cselectBox.options.add(option2);

        document.getElementById("details"+counter).innerHTML+="</p><p>"+counter+". Candidate Country";
        box.appendChild(cselectBox);

        var box2 = document.getElementById("detailsx"+counter);

        var ccselectBox = document.createElement("Select");
        ccselectBox.name="stateSelect";
        document.getElementById("detailsx"+counter).innerHTML+="</p><p>"+counter+". Candidate City";
        box2.appendChild(ccselectBox);
}

</script>
</head>

<body>
<form name="myForm" >
<input type="button" value="Add Candidate" onClick="ff(); populateDropdown(UnitedStates);"">
<!--- Note: 6 Candidates will be the maximum. -->
<div id="details1"><b></b></div>
<div id="detailsx1"><b></b></div>
<div id="details2"><b></b></div>
<div id="detailsx2"><b></b></div>
<div id="details3"><b></b></div>
<div id="detailsx3"><b></b></div>
<div id="details4"><b></b></div>
<div id="detailsx4"><b></b></div>
<div id="details5"><b></b></div>
<div id="detailsx5"><b></b></div>
<div id="details6"><b></b></div>
<div id="detailsx6"><b></b></div>
</form>
</body>
</html>
4

1 回答 1

0

这里有多个问题。我们将首先解决您要处理的问题。

当您命名多个具有相同名称的控件时,例如 stateSelect,您每次尝试引用它时都会得到第一个。如果您将 id 设置为'stateSelect' + counter,您将获得一个唯一的 id,然后您可以使用document.getElementById(). 因此,在填充下拉列表的函数中将如下所示:

function populateDropdown(arry)
{
    var stateSelect = document.getElementById('stateSelect'+counter);
    stateSelect.options.length = 0;

    for (var i = 0; i < arry.length; i++)
    {
        stateSelect.options[i] = new Option(arry[i], arry[i]);
    }
}

这是我用来验证这些更改的小提琴。

您还需要向每个国家/地区下拉列表添加一个事件,以便在状态下拉列表更改时重新填充它,并且该结构需要做一些工作。如果您不反对框架,淘汰赛将使这非常容易运行。

这是一切正常工作的小提琴,并在关键更改时添加了评论

更新:添加了指向小提琴的链接

于 2013-08-31T15:10:11.567 回答