简体   繁体   English

在 javascript 或 jquery 中从 html 下拉列表中获取值

[英]getting value from html drop down in javascript or jquery

i have this drop down in html which is created by java script i need to fetch value from these dynamically created down with the same or id and and post it to through ajax in php can you please help how to get value from these selected drop down我在 html 中有这个下拉列表,它是由 java 脚本创建的

         <select name="criteria[]">
                            <option value="null" disabled selected>- select -</option>
                            <option value="a">a</option>
                            <option value="b">b</option>

            <option value="c">c</option>

 <select name="criteria[]">
                            <option value="null" disabled selected>- select -</option>
                            <option value="a">a</option>
                            <option value="b">b</option>
                            <option value="c">c</option>
 </select>

so what you can do is loop through the select list using jquery .each() and store the values in the array.所以你可以做的是使用 jquery .each()遍历选择列表并将值存储在数组中。 here's what you can try.这是您可以尝试的方法。

$(function(){

    var array = [];
    $('#btnGetVal').on('click', function(){
        $('select[name="criteria[]"]').each(function(){
            console.log($(this).val());
            array.push($(this).val());
        });        

        console.log(array);

        $.ajax({
            type: 'post',
             url: 'yourfile.php',
            data: {values:array},
            success: function(d){
                // do something on success
            }
        });

    });


});

here's a working JSFIDDLE .这是一个有效的JSFIDDLE Please check the console for values.请检查控制台的值。 Hope this helps希望这可以帮助

Your question is not completely clear.你的问题不完全清楚。 You have two identical dropdown controls, with identical names/options.您有两个相同的下拉控件,具有相同的名称/选项。 Is this intentional?这是故意的吗? How do you wish to use them?您希望如何使用它们?

If both are required, it is a good idea to make them unique by assigning each a unique ID:如果两者都需要,最好通过为每个分配一个唯一的 ID 来使它们唯一:

<select id="s1" name="criteria[]">
    ...options...
</select>

You can then access their data easily:然后,您可以轻松访问他们的数据:

var sel1_val = $('#s1').val();

Because the HTML is created by javascript, you may need to use event delegation:由于 HTML 是由 javascript 创建的,因此您可能需要使用事件委托:

$(document).on('change', 'select[name="criteria[]"]', function(){
    var sel1_val = $('s1').val();
    var sel2_val = $('s2').val();
    $.ajax({
        type: 'post',
         url: 'your_php_ajax_processor_file.php',
        data: 'sel1=' +sel1_val+ '&sel2=' +sel2_val,
        success: function(d){
            if (d.length) alert(d);
        }
    });
});

In your PHP file:在您的 PHP 文件中:

<?php
    $s1 = $_POST['sel1'];
    $s2 = $_POST['sel2'];
    //now, do what you need to do with the two values

The .on() is necessary to detect events for injected markup. .on()是检测注入标记事件所必需的。

Note that if you are using AJAX (you are), then you don't need to use a <form> structure -- you can just use a normal div.请注意,如果您正在使用 AJAX(您正在使用),那么您不需要使用<form>结构——您可以只使用普通的 div。

See these additional posts for more on AJAX:有关 AJAX 的更多信息,请参阅这些附加帖子:

AJAX request callback using jQuery 使用 jQuery 的 AJAX 请求回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM