简体   繁体   English

单击时更改表单选择选项,然后选择另一个选项

[英]Change form select option when click and select another option

I have something like this until now. 到目前为止,我有这样的事情。

<form> 
       <select name='country'>
            <option value='0'>Select one country</option>
            <option value='germany'>Germany</option>
            <option value='england'>England</option>
       </select>
       <select name='city'>
            <option value=''>Select one city</option>
            <option value='stuttgart'>Stuttgard</option>
            <option value='munchen'>Munchen</option>
       </select>
       <input type='submit' name='submit' value='Search'>
</form>

What i want: when someone choose one country like germany, next option value should be germans cities. 我想要的是:当某人选择一个国家(例如德国)时,下一个选项值应该是德国城市。 But if i choose England i want to modify next option value and text to have something like this with english cities: 但是,如果我选择英格兰,我想修改下一个选项的值和文本,使英语城市具有以下内容:

     <select name='city'>
            <option value=''>Select one city</option>
            <option value='london'>London</option>
            <option value='manchester'>Manchester</option>
     </select>

I dont know how to do this, please inform me or redirect to some online stufs or tutorials, i dont want all the codes from you, but i have no idea. 我不知道该怎么做,请通知我或重定向到一些在线stufs或教程,我不希望收到您的所有代码,但我不知道。 Thanks! 谢谢!

If you're not using jQuery already you probably should be as tasks like this become much easier with it. 如果您还没有使用jQuery,您可能应该会因为这样的任务变得更加容易。 You'd want to do something like this: 您想做这样的事情:

$("select[name='country']").on("change",function(){
    var currentValue = $(this).val();
    var options = "";
    //switch on currentValue and build options string
    $("select[name='city']").html(options);
});

It is very depends on how you store the value and what javascript framework that you are using. 这很大程度上取决于您如何存储值以及所使用的JavaScript框架。 This is the very simple and pure javascript implementation. 这是非常简单和纯净的javascript实现。

 var c1 = ["City 1-1", "City 1-2"]; var c2 = ["City 2-1", "City 2-2"]; var x = [c1, c2]; function onCountryChanged(s) { var cities = document.getElementById("city"); if (s.value > 0) { var v = ""; for (var i = 0; i < x[s.value - 1].length; i++) { v += "<option value='" + x[s.value - 1][i] + "'>" + x[s.value - 1][i] + "</option>" } cities.innerHTML = v; } else { cities.innerHTML = ""; } } 
 <select onchange="onCountryChanged(this)"> <option value="0">Select Country</option> <option value="1">Country 1</option> <option value="2">Country 2</option> </select> <select id="city"> </select> 

  var countries = []; countries['germany'] = ["München", "Berlin", "Stuttgart"]; countries['england'] = ["London", "Manchester", "Liverpool"]; document.querySelector("select[name='country']").addEventListener("change", function(){ var element = countries[this.value.toString().toLowerCase()]; if (element) { //clone: var select = document.querySelector("select[name='city']").cloneNode(); var node = document.createElement("option"); node.value = 0; node.setAttribute("disabled", true); node.setAttribute("selected", true); node.textContent = "Select a city"; select.appendChild(node); countries[this.value.toString().toLowerCase()].forEach(function(element){ var node = document.createElement("option"); node.value = element; node.textContent = element; select.appendChild(node); }); document.querySelector("select[name='city']").parentElement.replaceChild(select, document.querySelector("select[name='city']")); } }, false); 
 <form> <select name='country'> <option value='0'>Select one country</option> <option value='germany'>Germany</option> <option value='england'>England</option> </select> <select name='city'> </select> <input type='submit' name='submit' value='Search'> </form> 

This will do it I guess. 我猜这会做到的。 I hope you can see what it does. 希望您能看到它的作用。

  1. Created arrays containing the cities. 创建了包含城市的数组。
  2. When the value of country select is changed, the event change fires. 更改国家/地区选择的值时,将触发事件更改。
  3. If the value matches an item from the countries array, then continue. 如果该值与“国家/地区”数组中的项匹配,则继续。
  4. Iterate over all values and append them to a cloned select. 遍历所有值,并将它们附加到克隆的选择中。
  5. Replace the old select with the new one. 用新的选择替换旧的选择。

I'm cloning the old select so we can add nodes in memory and then replace the whole select at once. 我正在克隆旧的选择,因此我们可以在内存中添加节点,然后立即替换整个选择。 This prevents the browser from redrawing every time a new option is added, saving resources. 这样可以防止浏览器每次添加新选项时都重新绘制,从而节省了资源。

Another way without jQuery, using data-attributes : 没有jQuery的另一种方式,使用data-attributes:

 var countrySelect = document.querySelector('[name=country]'); var cityOptions = document.querySelectorAll('option[data-country]'); countrySelect.addEventListener('change', function(){ for (var i = 0; i < cityOptions.length; i++){ var opt = cityOptions[i]; opt.style.display = (opt.getAttribute('data-country') === this.value) ? '' : 'none'; } }, false); 
 <select name='country'> <option value='0'>Select one country</option> <option value='germany'>Germany</option> <option value='england'>England</option> </select> <select name='city'> <option value=''>Select one city</option> <option data-country="england" value='london'>London</option> <option data-country="england" value='manchester'>Manchester</option> <option data-country="germany" value='stuttgart'>Stuttgard</option> <option data-country="germany" value='munchen'>Munchen</option> </select> 

您可以使用层次选择插件等: jquery-select-hierarchy

I found an solutions, is not very well optimized but it works: http://jsfiddle.net/YPsqQ/772/ 我找到了一个解决方案,虽然优化效果不是很好,但是可以正常使用: http : //jsfiddle.net/YPsqQ/772/

$(document).ready(function() {

$("#country").change(function() {
    var val = $(this).val();
    if (val == "1") {
        $("#city").html("<option value='berlin'>Berlin</option><option value='munchen'>Munchen</option>");
    } else if (val == "2") {
        $("#city").html("<option value='london'>London</option><option value='manchester'>Manchester</option>");

    } 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击另一个框中的选项时,更改选择框中的选项 - Change options in select box when click option in another box 由于另一个选项,更改 select 选项 - Change a select option thanks to another option 在选择选项上更改表单操作 - Change form action on select option jQuery-当另一个选择更改时,更改选择的select选项 - JQuery - Change selected option of select when another select changed 根据提交按钮上的选择字段选项更改表单操作 - Change form action based on select field option on submit button click 从另一个选择中更改选择标记中的选项 - Change option in select tag from another select 在AngularJS中更改另一个选择选项的值时,如何更改选择选项的值? - How can I change a select option value when I change the value of another select option in AngularJS? 当我单击同一选择的另一个选项时,如何才能获得多个选择的未选择选项? - how I can get unselected an option in a select multiple when I click in another option of that same select? 单击 Select 选项时如何更改箭头方向 - How to change arrow direction when click in Select option 当我选择另一个选择标记HTML的某个选项时,如何自动更改选择标记选项的值列表<select> - How to automatically change the value list of option of select tag when i select the certain option of another select tag HTML <select>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM