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

标签 javascript jquery ajax

这是我学习 PHP 的第一周(所以不要欺负人)。我正在尝试获取选择表单的值并将其插入到我的数据库中。插入内容很好,但我似乎无法传递该值。

脚本:

<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>

和 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>

除了格式错误之外,我还做错了什么?

最佳答案

select.rank将选择 select 类型的元素这是 rank 的成员类。

<select name="rank">不是任何类的成员。它没有类属性。

您需要更改选择器以使其与元素匹配,反之亦然。

关于javascript - 如何在AJAX中获取选择表单的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16384837/

相关文章:

jquery - 对 WrappedNative 原型(prototype)对象的非法操作

javascript - 如何从超链接将调用的事件发送到 JS 函数

javascript - 可以存储id的标签编辑器

javascript - JQuery 日期选择器功能不起作用

Javascript/Jquery - 加法和减法错误

javascript - 使用 execCommand 删除一个元素,使其不可撤销

jquery - 通过自动页面滚动,单击时将 div 浮出窗口顶部

javascript - jQuery 从一个函数到另一个函数访问全局变量的值

javascript - 正则表达式 unicode 范围 [uXXXX] 未使用 ajax 识别

javascript - jQuery 判断表单下是否存在某个元素