在未实现接口HTMLInputElement的对象上调用了'stepUp'

use*_*186 2 javascript ajax jquery

我有一个ID为#country的国家/地区下拉列表。我试图将作为ajax请求选择的值传递给php文件-countrycode.php,并将接收到的值传递给ID为#tele的另一个输入字段。我的代码如下:

<script>
$('#country').change(function() {
    //var country = $(this).val();
    var country = $('#country').val();
    //alert(country);
});
                $.ajax({
                    type: "POST",
                    url: 'countrycode.php',
                   data: { country : country },
                    success: function(data)
                    {
                       $("#tele").html(data);
                    }
                });
</script>
Run Code Online (Sandbox Code Playgroud)

警报(国家);显示正确的所选国家。我尝试使用:

var country = $(this).val();
Run Code Online (Sandbox Code Playgroud)

也带有正确的警报消息。

问题在于:

data: { country : country },
Run Code Online (Sandbox Code Playgroud)

我收到错误:TypeError:'stepUp'在未实现接口HTMLInputElement的对象上调用

我尝试了Stackoverflow ..中的所有答案,但是不明白为什么它不起作用?

a.u*_*u.b 5

您的country变量在change函数内部。因此对于ajax它将是未定义的。我认为这必须是全球性的。

你可以试试这个吗?

<script>
      var country;
      $('#country').change(function() {
            //var country = $(this).val();
            country = $('#country').val();
            //alert(country);
            $.ajax({
                type: "POST",
                url: 'countrycode.php',
                data: { country : country },
                success: function(data)
                      {
                         $("#tele").val(data);
                      }
            });
      });
</script>
Run Code Online (Sandbox Code Playgroud)