jQuery get value of select onChange

后端 未结 16 1502
星月不相逢
星月不相逢 2020-11-22 05:31

I was under the impression that I could get the value of a select input by doing this $(this).val(); and applying the onchange parameter to the sel

相关标签:
16条回答
  • 2020-11-22 05:57

    I want to add, who needs full custom header functionality

       function addSearchControls(json) {
            $("#tblCalls thead").append($("#tblCalls thead tr:first").clone());
            $("#tblCalls thead tr:eq(1) th").each(function (index) {
                // For text inputs
                if (index != 1 && index != 2) {
                    $(this).replaceWith('<th><input type="text" placeholder=" ' + $(this).html() + ' ara"></input></th>');
                    var searchControl = $("#tblCalls thead tr:eq(1) th:eq(" + index + ") input");
                    searchControl.on("keyup", function () {
                        table.column(index).search(searchControl.val()).draw();
                    })
                }
                // For DatePicker inputs
                else if (index == 1) {
                    $(this).replaceWith('<th><input type="text" id="datepicker" placeholder="' + $(this).html() + ' ara" class="tblCalls-search-date datepicker" /></th>');
    
                    $('.tblCalls-search-date').on('keyup click change', function () {
                        var i = $(this).attr('id');  // getting column index
                        var v = $(this).val();  // getting search input value
                        table.columns(index).search(v).draw();
                    });
    
                    $(".datepicker").datepicker({
                        dateFormat: "dd-mm-yy",
                        altFieldTimeOnly: false,
                        altFormat: "yy-mm-dd",
                        altTimeFormat: "h:m",
                        altField: "#tarih-db",
                        monthNames: ["Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık"],
                        dayNamesMin: ["Pa", "Pt", "Sl", "Ça", "Pe", "Cu", "Ct"],
                        firstDay: 1,
                        dateFormat: "yy-mm-dd",
                        showOn: "button",
                        showAnim: 'slideDown',
                        showButtonPanel: true,
                        autoSize: true,
                        buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
                        buttonImageOnly: false,
                        buttonText: "Tarih Seçiniz",
                        closeText: "Temizle"
                    });
                    $(document).on("click", ".ui-datepicker-close", function () {
                        $('.datepicker').val("");
                        table.columns(5).search("").draw();
                    });
                }
                // For DropDown inputs
                else if (index == 2) {
                    $(this).replaceWith('<th><select id="filter_comparator" class="styled-select yellow rounded"><option value="select">Seç</option><option value="eq">=</option><option value="gt">&gt;=</option><option value="lt">&lt;=</option><option value="ne">!=</option></select><input type="text" id="filter_value"></th>');
    
                    var selectedOperator;
                    $('#filter_comparator').on('change', function () {
                        var i = $(this).attr('id');  // getting column index
                        var v = $(this).val();  // getting search input value
                        selectedOperator = v;
                        if(v=="select")
                            table.columns(index).search('select|0').draw();
                        $('#filter_value').val("");
                    });
    
                    $('#filter_value').on('keyup click change', function () {
                        var keycode = (event.keyCode ? event.keyCode : event.which);
                        if (keycode == '13') {
                            var i = $(this).attr('id');  // getting column index
                            var v = $(this).val();  // getting search input value
                            table.columns(index).search(selectedOperator + '|' + v).draw();
                        }
                    });
                }
            })
    
        }
    
    0 讨论(0)
  • 2020-11-22 06:05

    This is helped for me.

    For select:

    $('select_tags').on('change', function() {
        alert( $(this).find(":selected").val() );
    });
    

    For radio/checkbox:

    $('radio_tags').on('change', function() {
        alert( $(this).find(":checked").val() );
    });
    
    0 讨论(0)
  • 2020-11-22 06:06

    only with JS

     let select=document.querySelectorAll('select') 
      select.forEach(function(el) {
        el.onchange =  function(){
         alert(this.value);
          
      }}
      )
    
    0 讨论(0)
  • 2020-11-22 06:07

    I was under the impression that I could get the value of a select input by doing this $(this).val();

    This works if you subscribe unobtrusively (which is the recommended approach):

    $('#id_of_field').change(function() {
        // $(this).val() will work here
    });
    

    if you use onselect and mix markup with script you need to pass a reference to the current element:

    onselect="foo(this);"
    

    and then:

    function foo(element) {
        // $(element).val() will give you what you are looking for
    }
    
    0 讨论(0)
提交回复
热议问题