简体   繁体   English

如何在AJAX中获取选择表单的值?

[英]How to get the value of a select form in AJAX?

This is my first week of PHP (so no bullying). 这是我使用PHP的第一周(不要欺负)。 I'm trying to grab the value of a select form and insert into my db. 我正在尝试获取选择表单的值并将其插入到我的数据库中。 The insert is all good but I can't seem to pass the value. 插入效果很好,但我似乎无法传递该值。

Script: 脚本:

<script type="text/javascript">
     //add to top 10
    function addTop10()
    {
    var show_id = $('#show_id').val();  
    var user_id = $('#user_id').val();
    var rank = $('select.rank').val();
        //Storing the value of textbox into a variable
    if(show_id == '')        //Checking for NULL
    {
    $('#propspectDiv').html('Enter A Valid Name');    //Prints the progress text into our Progress DIV
    $('#show_id').addClass('error');                    //Adding the error class to the progress DIV
    return;
    }
    else{
    $('#show_id').removeClass('error');
    $('#propspectDiv').removeClass('error'); //Removing the error class from the progress DIV
    $('#propspectDiv').html('Submitting your Request.<img src="ajax.gif" />');//Prints the progress text into our Progress DIV

    $.ajax({
    url : 'top10ajax.php', //Declaration of file, in which we will send the data
    data:{
    "show_id" : show_id,  
    "user_id" : user_id,
    "rank" : rank          //we are passing the name value in URL
    },
    success : function(data){
    window.setTimeout(function(){
    $('#propspectDiv').html('Added'); //Prints the progress text into our Progress DIV
    }, 2000);
    }
    });
    }
    }

    </script>

and the html 和HTML

<div class="row">
        <div class="twelve columns">
<h4>Add to your top 10 </h4>
<div class="two columns">
    <form>
<select name="rank">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
   <option value="7">7</option>
   <option value="8">8</option>
  <option value="9">9</option>
   <option value="10">10</option>

</select>
</div>
<div class="four columns">
         <button class="large button" id="rank" type="button" value="Add to Top 10" onclick="addTop10()"></form>
</div>

What am I doing wrong, besides bad formatting? 除了格式错误外,我在做什么错?

select.rank will select an element of type select that is a member of the rank class. select.rank将选择属于rank类成员的select类型的元素。

<select name="rank"> isn't a member of any classes. <select name="rank">不是任何类的成员。 It has no class attribute. 它没有类属性。

You need to either alter your selector so it matches the element or vice versa. 您需要更改选择器,使其与元素匹配,反之亦然。

您尝试以错误的方式获取选择框的值,请尝试使用此方法

var rank = $("select['name=rank']").val();

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

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