문제는 배열에 텍스트가 추가되지 않고 있다는 것입니다. 코드는 괜찮아 보이지만 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() 함수로 문제를 식별 할 수 있습니까?
'내가 <= countryArray.length'가
나는이 쉽게 찾을 수 잘못된 조건. –