如何将输出值显示为文本?

时间:2016-03-18 03:19:54

标签: javascript jquery html css

在下面的代码中,选择后,结果值显示为弹出窗口。如何更改它以在第二个选择列表附近显示为文本?

这是小提琴:https://jsfiddle.net/ajolnir/dw0hd8zn/



 var countrieslist=document.classic.countries
        var citieslist=document.classic.cities
         
        var cities=new Array()
        cities[0]=""
        cities[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
        cities[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
        cities[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue", "Birmingham|birminghamvalue"]
         
        function updatecities(selectedcitygroup){
            citieslist.options.length=0
            if (selectedcitygroup>0){
                for (i=0; i<cities[selectedcitygroup].length; i++)
                    citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
            }
        }
&#13;
 <form name="classic">
        <select name="countries" size="4" onChange="updatecities(this.selectedIndex)" style="width: 150px">
        <option selected>Select A City</option>
        <option value="usa">USA</option>
        <option value="canada">Canada</option>
        <option value="uk">United Kingdom</option>
        </select>
         
        <select name="cities" size="4" style="width: 150px" onClick="alert(this.options[this.options.selectedIndex].value)">
        </select>
        </form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

alert()替换为innerHTML(其中span #result是目标)

&#13;
&#13;
 var countrieslist=document.classic.countries
        var citieslist=document.classic.cities
         
        var cities=new Array()
        cities[0]=""
        cities[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
        cities[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
        cities[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue", "Birmingham|birminghamvalue"]
         
        function updatecities(selectedcitygroup){
            citieslist.options.length=0
            if (selectedcitygroup>0){
                for (i=0; i<cities[selectedcitygroup].length; i++)
                    citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
            }
        }

function showresult(key) {
document.getElementById("result").innerHTML = key; 
}
&#13;
<form name="classic">
        <select name="countries" size="4" onChange="updatecities(this.selectedIndex)" style="width: 150px">
        <option selected>Select A City</option>
        <option value="usa">USA</option>
        <option value="canada">Canada</option>
        <option value="uk">United Kingdom</option>
        </select>
         
        <select name="cities" size="4" style="width: 150px" onClick="showresult(this.options[this.options.selectedIndex].value)">
        </select>
        </form>
<span id=result style="font-size: 2em;"><span>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个。将jquery onlie添加到你的html.I更新你的小提琴

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<form name="classic">
<select name="countries" size="4" onChange="updatecities(this.selectedIndex)" style="width: 150px">
<option selected>Select A City</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="uk">United Kingdom</option>
</select>
 
<select id="sone" name="cities" size="4" style="width: 150px" onClick="alert(this.options[this.options.selectedIndex].value)">
</select>
<h3 id="displayme">show me</h3>

</form>
 
<script type="text/javascript">
 
var countrieslist=document.classic.countries
var citieslist=document.classic.cities
 
var cities=new Array()
cities[0]=""
cities[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
cities[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
cities[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue", "Birmingham|birminghamvalue"]
 
function updatecities(selectedcitygroup){
    citieslist.options.length=0
    if (selectedcitygroup>0){
        for (i=0; i<cities[selectedcitygroup].length; i++)
            citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
    }
}
 
</script>

然后在<script>

中写下
$(document).ready(function(){
    $("#sone").on('change',function(){
    var selected = $(this).val();
    $("#displayme").text(selected);
  });
});

jsfiddle:https://jsfiddle.net/dw0hd8zn/2/