【问题标题】:Problem in using jQuery dependent country state dropdown使用 jQuery 依赖国家/地区下拉列表的问题
【发布时间】:2022-12-03 02:23:41
【问题描述】:

jQuery 不适用于单选(第一个选项国家/地区从下拉列表中选择,第二个国家/地区从数据库中获取)我用于多选的相同数据库工作正常但单选不工作请帮助!!!

mysql数据库是 state_id country_id state_name 德里 印度 德里

<label for="country">Country</label> 
<?php include "fetch_country.php"; ?>
<select id="country" name="country"  class="form-control" >
<option value="India" label="India">India</option>
<option value="USA" label="USA">USA</option>
<option value="UK" label="UK">UK</option>
<option value="Canada" label="Canada">Canada</option>
</select>

   
<label for="state">State</label>

<select id="state" name="state"  class="form-control" >
<option disabled>Select Country First</option> 
</select>





single select not working i try this 
( Not working on single select !!)

<script>
$(document).ready(function(){

$('#country').on('change', function(){
 
onChange:function(option, checked){
var selected = this.$select.val();
if(selected.length > 0)
{
$.ajax({
url:"fetch_country.php",
method:"POST",
data:{selected:selected},
success:function(data)
{
$('#state').html(data);
}
})
}
}
});
}); 
</script>


Pls see multiselect script which is working fine but i want to change this into single select 
$(document).ready(function(){

$('#country').multiselect({
nonSelectedText:'?',
buttonWidth:'250px',
maxHeight: 400,
onChange:function(option, checked){
var selected = this.$select.val();
if(selected.length > 0)
{
$.ajax({
url:"fetch_country.php",
method:"POST",
data:{selected:selected},
success:function(data)
{
$('#state').html(data);
$('#state').multiselect('rebuild');
}
})
}
}
});
$('#state').multiselect({
nonSelectedText: '?',
allSelectedText: 'All',
buttonWidth:'250px', 
includeSelectAllOption: true,
maxHeight: 400,
enableFiltering:true
});
});

【问题讨论】:

  • 欢迎来到堆栈溢出。 change 事件仅在用户选择替代选项时触发。当页面加载时,只有一个选项,因此用户无法选择其他选项。您可能希望在页面加载时获取列表,以便在用户打开 Select 元素时列出所有选项。

标签: javascript php jquery select fetch


【解决方案1】:

考虑以下。

$(function() {
  $('#country').change(function() {
    var selected = $(this).val();
    if (selected.length > 0) {
      $.ajax({
        url: "fetch_country.php",
        method: "POST",
        data: {
          selected: selected
        },
        success: function(data) {
          $('#state').html(data);
        }
      })
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="country" name="country" class="form-control">
  <option disabled selected>Select Country</option>
  <option>USA</option>
</select>
<select id="state" name="state" class="form-control">
  <option disabled selected>Select Country First</option>
</select>

您的代码未显示 #country 元素。我将它添加到这个示例中,以便在对其进行更改时触发 AJAX 调用。我无法对此进行进一步测试。

要获取元素的值,请使用 $(this).val()。其余的看起来应该可以工作。

【讨论】:

  • 我试试这个但没有用
  • @ravi 你收到错误了吗?控制台里有什么?什么不起作用?
  • #Country Selection 来自下拉菜单,第二个下拉菜单 #state 是从数据库中获取的,我试过你的例子,但不起作用请帮忙!!
  • 实际上没有错误首先下拉是一个选项,它依赖于状态但不从数据库中获取状态列表,我在多选上使用相同的脚本但我希望这是单选......多选脚本工作正常----
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 2011-10-13
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 2012-08-16
相关资源
最近更新 更多