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

'stepUp' called on an object that does not implement interface HTMLInputElement

本文关键字:调用 HTMLInputElement 对象 stepUp 实现 接口      更新时间:2023-09-26

我有一个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>

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

var country = $(this).val();

也有正确的警报信息。

问题出在:

data: { country : country },

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

我尝试了Stackoverflow中的所有答案。。但无法理解它不起作用的原因????

您的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>

如果在country字段更改时尝试使用Ajax对字段进行udpate,则需要将Ajax调用放在.change()处理程序中,以便在此时调用它。

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

正如您以前所做的那样,在country变量中有任何值之前,您只在启动时调用ajax函数,然后当.change()事件发生时,您根本没有调用ajax函数。