如何从下拉列表中选择选项并执行 onchange Jquery

发布于 2024-11-04 19:49:36 字数 2218 浏览 1 评论 0原文

我可能以最糟糕的方式这样做,但我试图a)根据从社交网络插件接收到的数据选择一个下拉选项,b)强制onchange事件使连续的下拉菜单出现并填充。

我正在处理国家、州和城市的数据。页面加载后,国家/地区下拉列表会自动填充两个选择:墨西哥和美国。选择一个州后,会自动填充州下拉列表,选择一个州后,会填充城市下拉列表。

这是标记:

<tr>
<td id="field-country" class="tags-form"></td>
   <td><select onchange="CreateCountries.getRegions(this.value);"
name="country_id" id="country_id" value="" style="width: 130px;"
class="campos-tabla"></select></td>
</tr>
<tr>
   <td id="field-state" class="tags-form"></td>
   <td><select onchange="CreateCountries.getCities(this.value);"
name="state_id" id="state_id" value="" style="width: 130px;"
class="campos-tabla"></select></td>
</tr>
<tr>
   <td id="field-city" class="tags-form"></td>
   <td><select name='city_id' id="city_id" value=""class="campos-tabla"
style="width: 130px;"></select></td>
</tr>

这是我的尝试:a)根据收到的数据选择下拉选项,b)激活 onchange 事件,以便填充下一个下拉菜单,然后继续我的下一个选择:

_fillFields: function(){
 logingygia.fields.firstname.val(logingygia.firstname);
 logingygia.fields.lastname.val(logingygia.lastname);
 logingygia.fields.email.val(logingygia.email);
  if (logingygia.gender == 'm') {
    $('#genderM').attr('checked',true);
        }
        else {
            $('#genderF').attr('checked',true);
        };
 logingygia.fields.birthmonth.val(d2(logingygia.birthmonth));
 logingygia.fields.birthday.val(d2(logingygia.birthday));
 logingygia.fields.birthyear.val(logingygia.birthyear)
if (logingygia.country == 'United States') {
        $('#country_id').val(212);
            }
            else {
                $('#country_id').val(143);
            };
            if $('#country_id option: selected ').length){

                if ($('#country_id option:selected').length)
                    $('#country_id').val("state_id).change();
                                }else{
                                  return ("");
            };
            }
};

if (logingygia.country 本身将不幸的是,当我将其与“ifcountry_id:选项”结合使用时,没有选择任何国家,并且州下拉列表将仅显示“Selecciona”,但不会填充任何选择......这是有道理的,因为 我确信代码很混乱,

所以我非常感谢一些更好的人来帮助我。

I'm probably doing this in the worst way but I'm trying to a) select a dropdown option based on received data from a social networking plugin and b) force an onchange event to make the successive dropmenus appear and populate.

I'm working with country, state and city data. Upon page load, the country dropdown auto populates with two selections: Mexico and United states. When one is selected, the state dropdown auto populates and once a state is selected the city dropdown populates.

here's the markup:

<tr>
<td id="field-country" class="tags-form"></td>
   <td><select onchange="CreateCountries.getRegions(this.value);"
name="country_id" id="country_id" value="" style="width: 130px;"
class="campos-tabla"></select></td>
</tr>
<tr>
   <td id="field-state" class="tags-form"></td>
   <td><select onchange="CreateCountries.getCities(this.value);"
name="state_id" id="state_id" value="" style="width: 130px;"
class="campos-tabla"></select></td>
</tr>
<tr>
   <td id="field-city" class="tags-form"></td>
   <td><select name='city_id' id="city_id" value=""class="campos-tabla"
style="width: 130px;"></select></td>
</tr>

and here's my attempt to a) select dropdown option based on data received and b) activate the onchange event so that the next dropdown menu will populate and then continue with my next selection:

_fillFields: function(){
 logingygia.fields.firstname.val(logingygia.firstname);
 logingygia.fields.lastname.val(logingygia.lastname);
 logingygia.fields.email.val(logingygia.email);
  if (logingygia.gender == 'm') {
    $('#genderM').attr('checked',true);
        }
        else {
            $('#genderF').attr('checked',true);
        };
 logingygia.fields.birthmonth.val(d2(logingygia.birthmonth));
 logingygia.fields.birthday.val(d2(logingygia.birthday));
 logingygia.fields.birthyear.val(logingygia.birthyear)
if (logingygia.country == 'United States') {
        $('#country_id').val(212);
            }
            else {
                $('#country_id').val(143);
            };
            if $('#country_id option: selected ').length){

                if ($('#country_id option:selected').length)
                    $('#country_id').val("state_id).change();
                                }else{
                                  return ("");
            };
            }
};

The if (logingygia.country by itself will select the country correctly. Unfortunately, when I combine that with the "if country_id: option", no country is selected and the state dropdown will show up with only "Selecciona" but does not populate with any choices ... which makes sense since country was not selected.

I'm sure the code is a mess so I would greatly appreciate some better minds to help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-11-11 19:49:36

你在 if 上缺少一个括号,并且我不确定你是否可以在 : 之后有一个空格。另外,你还缺少一个报价。如果有两次,你也会有同样的情况。

if ($('#country_id option:selected').length){
   $('#country_id').val('state_id').change();
}else{
   return ("");
};

修复语法并查看是否可以解决问题。

You're missing a parenthese on the one if, and I'm not sure if you can have a space after the :. Also, you're missing a quote. And you have the same if twice.

if ($('#country_id option:selected').length){
   $('#country_id').val('state_id').change();
}else{
   return ("");
};

Fix up the syntax and see if that fixes it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文