我遇到的问题是我的文本由于某种原因没有被添加到数组中。代码看起来不错,但我可以通过打印出 array.length 来确认它没有被添加。
<script>
function addradio(value)
{
var element = document.createElement("input");
var label = document.createElement("label");
var tele = document.getElementById("Telephone");
var selected_text = tele.options[tele.selectedIndex].innerHTML;
// Array uses to stop duplicate radio button
var countryArray = new Array();
// Used to check if country is already in array, defaults to false
var contain = new Boolean();
// Checks if text contains a "1", returns "-1" if it doesn't
var str = selected_text.indexOf("1");
// For loop to check if country name is already in the array
for(var i = 0; i <= countryArray.length; i++)
{
if(countryArray[i] == selected_text)
contain = true;
else
contain = false;
}
// If value is not empty and text does not contain a "1" and array doesn't contain specified country
if(value != "" && str == "-1" && contain == false)
{
// Creating the attributes for a radio button
element.setAttribute("type", "radio");
element.setAttribute("value", value);
element.setAttribute("name", "telephone");
label.appendChild(element);
label.innerHTML += selected_text + "<br>";
// Creating the radio button
var newradio = document.getElementById("fillme");
newradio.appendChild(label);
// Adds country into the array if its' not already there
countryArray.push(selected_text);
}
}
</script>
任何人都可以识别我的 Array.push() 函数的问题吗?