【问题标题】:Pass a select value in ajax在ajax中传递一个选择值
【发布时间】:2019-11-17 05:23:50
【问题描述】:

我正在尝试在我的 ajax 函数中传递一个选择值。

我的html代码:

    <select class="form-control" id="exampleFormControlSelect1" class="selectUser">
                    <?php foreach($logs as $key => $value){ ?>
                        <option value="<?php echo $value['username'];?>"><?php echo $value['username'];     ?></option>
                    <?php } ?>
                </select>

和我的 JS 代码:

   $('select.selectUser').change(function(){
        alert('Select field value has changed to' + $('select.selectUser').val());

        $.ajax({
            type: 'GET',
            url: "Panier/loadPanier",
            data: {username: $('select.selectUser').val()},
            success: function(result){
                var data1 = JSON.parse(result);
                alert(data1.username) ;
            },

        });

    });

但第一个 alert('Select field value has changed to' + $('select.selectUser').val()); 没有生成任何警报,我的控制台也没有收到任何错误。

你能告诉我有什么问题吗?

【问题讨论】:

  • 尝试仅使用 $(".selectUser") 类另外尝试检查选项标签是否实际上具有值

标签: html ajax select


【解决方案1】:

让我们试试$('#exampleFormControlSelect1').on('change', function(){

【讨论】:

  • 可能是评论
【解决方案2】:

您已将 two class 属性添加到 select 元素 - class="form-control"class="selectUser".

<select class="form-control" id="exampleFormControlSelect1" class="selectUser">

因此,它忽略了您的第二堂课 - class="selectUser"

$('select.selectUser').on('change', function() {
    console.log($(this).val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control selectUser" id="exampleFormControlSelect1">
  <option value="1" selected="selected">One</option>
  <option value="2">Two</option>
</select>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多