捕获多个值使用javascript选择多个

时间:2014-12-03 22:29:57

标签: javascript php jquery ajax forms

我的问题很简单,但我无法修复它,基本上需要捕获一个字段的值"选择多个"。 代码是要点。

Gist Code

<script type="text/javascrit">
function newsletter_send( name, email, category register ) {
    jQuery.ajax({
        type: "POST",
        url: alm_localize.ajaxurl,
        data: {
            action      : "newsletter_register", /// function for register data in DB.
            user_name   : name,
            user_email  : email,
            user_category   : category, // Here you can find the values of the select
            user_register   : register
        },
        success: function(data, textStatus, XMLHttpRequest) {
            if( email !== "" && name !== "" && register !== "" ) {
                $( "#newsletter_form" ).find("#feedback").html("<span class='success'Seu email foi registrado com sucesso!</span>");
            }
            $( "#newsletter_form" ).find( ".form_loading" ).removeClass( "show" );
        //  _newsletter_reset(); // reset values form
        },
        error: function(MLHttpRequest, textStatus, errorThrown) {
            $( "#newsletter_form" ).find("#contactResponse").html("<span class='error'>Algo deu errado, tente novamente mais tarde.</span>");
            $( "#newsletter_form" ).find( ".form_loading" ).removeClass( "show" );
        }
    });
}
</script>

<!--  THIS HTML -->

<select id="user_category" name="user_category[]" class="user_category" multiple="">
    <option selected="selected">Selecione uma categoria</option>
    <option value="4">Notícias</option>
    <option value="6">Santa Catarina</option>
    <option value="5">Saúde</option>
    <option value="1">Sem categoria</option>
</select>

<?php

// yeah, i use wordpress :p

/*
 * Function for register data in mysql
 */

function newsletter_register() {

    $user_email = $_POST["user_email"];
    $user_name  = $_POST["user_name"];
    $user_category  = implode(',', $_POST["user_category"]);
    $user_register  = $_POST["user_register"];

    global $wpdb;

    if(
        isset( $user_name )
        || isset( $user_email )
        || isset( $user_category )
    ) {
        $wpdb->insert( $wpdb->prefix . "newsletter", array(
            "user_name" => $user_name,
            "user_email"    => $user_email,
            "user_category" => $user_category,
            "user_register" => $user_register
        ));
    }
}

add_action( "wp_ajax_nopriv_newsletter_register", "newsletter_register" );
add_action( "wp_ajax_newsletter_register", "newsletter_register" );
?>

简单的借口。

1 个答案:

答案 0 :(得分:0)

$('select').val()select时,

multiple将返回一个数组。

  

来自val() API Docs:

     

对于select元素,当没有选择任何选项时返回null,并且当存在至少一个选项时,它返回包含每个所选选项的值的数组,并且由于存在multiple属性,因此可以选择更多。

示例:

$(function() {
  $('textarea').val(JSON.stringify($('select').val()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select multiple>
  <option value="1" selected>Item 1</option>
  <option value="2">Item 2</option>
  <option value="3" selected>Item 3</option>
  <option value="4">Item 4</option>
  <option value="5">Item 5</option>
  <option value="6" selected>Item 6</option>
</select>
<br>
<br>
<textarea></textarea>