简体   繁体   English

通过名称获取选择框并通过相同名称设置另一个选择框?

[英]Get a select box by name and set another select box by same name?

I am building a fairly complex form-- I need to copy some data between one and another and I am using jQuery to do this. 我正在构建一个相当复杂的表单-我需要在彼此之间复制一些数据,并且正在使用jQuery来做到这一点。 The only road block I am running into is setting the state. 我遇到的唯一障碍是设置状态。

I have two drop downs, one us using the full state name as the value and the other is using the state abbreviation as the value. 我有两个下拉菜单,一个是我们使用完整的州名作为值,另一个是使用状态缩写作为值。 The names are the same- 名称是相同的-

so on form 1 it looks like 所以在表格1上看起来像

 <option value="Illinois">Illinois</option>

and form 2 it looks like 而形式2看起来像

<option value="IL">Illinois</option>

Each form has its own unique css selector. 每种形式都有其自己独特的CSS选择器。 How can I set the selected value of form 2 to match what is in form 1 using jQuery? 如何使用jQuery设置表格2的选定值以匹配表格1中的值?

I do not have any control over the forms, just need to manipulate the input. 我对表单没有任何控制,只需要操纵输入即可。 Have tried using a name selector in jQuery, but I'm not having any luck. 曾尝试在jQuery中使用名称选择器,但我没有任何运气。

Thank you. 谢谢。

You can do something like this 你可以做这样的事情

<select id="fullName">
  <option value="Maryland" data-abbr="MD">Maryland</option>
  <option value="Illinois" data-abbr="IL">Illinois</option>
  <option value="Delaware" data-abbr="DE">Delaware</option>
</select>

<select id="abbr">
  <option value="MD">Maryland</option>
  <option value="IL">Illinois</option>
  <option value="DE">Delaware</option>
</select>

And your jQuery 还有你的jQuery

$('body').on('change', '#fullName', function(){
  var abbr = $(this).find('option:selected').data('abbr');
  $('#abbr').val(abbr);
});

Try this 尝试这个

<form id="form1" name="form1">
    <select name="states" onchange="changeselect(this)">
        <option value="option1">option1</option>
        <option value="option2">option2</option>
        <option value="option3">option3</option>
        <option value="option4">option4</option>
        <option value="option5">option5</option>
    </select>
</form>
<form id="form2" name="form2">
    <select name="states">
        <option value="opt1">option1</option>
        <option value="opt2">option2</option>
        <option value="opt3">option3</option>
        <option value="opt4">option4</option>
        <option value="opt5">option5</option>
    </select>
</form>

    function changeselect(elem)
    {
        var value1 = $(elem).val();
        $('#form2 select option').removeAttr('selected');
        $('#form2').find('select option').each(function(){
            var value2 = $(this).html();
            if(value1 == value2)
            {
                var selected = $(this).attr('value');
                $('#form2 select').val(selected);
            }
        });
    }

If you create 2 arrays which exactly correspond with one another: 如果创建2个彼此完全对应的数组:

var StateNames = ['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming'];

var StateAbbreviations = ['AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'];

You can: 您可以:

  • get the value from the first option list; 从第一个选项列表中获取value
  • find that value's index in the first array; 在第一个数组中找到该值的index ( hint: use indexOf ) 提示:使用 indexOf
  • use the same index to find out what the corresponding abbreviation is in the second array; 使用相同的index找出第二个数组中相应的缩写;
  • use the returned abbreviation to locate the correct option in the second option list 使用返回的缩写在第二个选项列表中找到正确的选项

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM