如果尚未列出该选项,则向选择框添加选项

时间:2015-02-17 15:54:02

标签: javascript

我需要你的帮助,

如何在现有的javascript代码中另外添加功能,这样当field1中提供的新值未在选择框中列出时,代码将:

  1. 检查(field1)值是否已在选择框中列出,如果是,则在(field2)中选择其值;

  2. 如果field1中提供的值未在选择框(field2)中列出,则从(field1)添加新值并在(field2)中选择

  3. 这是一个小提琴:http://jsfiddle.net/s66qg6xp/

    代码:

    <!DOCTYPE html>
    <html>
        <head>
            <script type="text/javascript">
                function test() {
                    document.getElementById('field2').value = document.getElementById('field1').value
                }
            </script>
        </head>
        <body>
            <input type="text" id="field1">
            <br>
            <select id="field2">
                <option value="apples">apples</option>
                <option value="oranges">oranges</option>
                <option value="bananas">bananas</option>
            </select>
            <br>
            <input type="button" value="test" onclick="test()">
        </body>
    </html>
    

2 个答案:

答案 0 :(得分:0)

所有在JS:

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript">

function test() {

var alreadyInList = [];
var allOptions = document.getElementsByTagName('option');
// populate already in list
for(var i = 0; i < allOptions.length; i++) {
    alreadyInList[i] = allOptions[i].value;
}

var input = document.getElementById('field1').value;
for(var i = 0; i < allOptions.length; i++) {
    if(alreadyInList.indexOf(input) > -1) {
        // element is already in the list
    } else {
        alreadyInList.push(input);
        var option = document.createElement('option');
        option.value = input;
        option.text = input;
        document.getElementById('field2').appendChild(option);
    }
}

}


</script>

</head>

<body>

<input type="text" id="field1">
<br>
<select id="field2">
<option value="apples">apples</option>
<option value="oranges">oranges</option>
<option value="bananas">bananas</option>
</select>
<br>
<input type="button" value="test" onclick="test()">

</body>

</html>

答案 1 :(得分:0)

或者你可以用jQuery做到这一点:

<!DOCTYPE html>
<html>
  <head>

    <script src="jquery-2.1.3.min.js"></script>

    <script type="text/javascript">
       function test() {
            var NewOption=document.getElementById("field1").value;
            var options=$("#field2").children().filter(function (){return $(this).text()==NewOption});
            if (options.length==0){                
              document.getElementById("field2").innerHTML=document.getElementById("field2").innerHTML+"<option value='"+NewOption+"'>"+NewOption+"</option>";
            }
            document.getElementById('field2').value = document.getElementById('field1').value;
        }



    </script>
</head>
<body>
    <input type="text" id="field1">
    <br>
    <select id="field2">
        <option value="apples">apples</option>
        <option value="oranges">oranges</option>
        <option value="bananas">bananas</option>
    </select>
    <br>
    <input type="button" value="test" onclick="test()">
</body>