如何从下拉列表中选择选项并执行 onchange Jquery
我可能以最糟糕的方式这样做,但我试图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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在 if 上缺少一个括号,并且我不确定你是否可以在 : 之后有一个空格。另外,你还缺少一个报价。如果有两次,你也会有同样的情况。
修复语法并查看是否可以解决问题。
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.
Fix up the syntax and see if that fixes it.