简体   繁体   English

选择框以发送到Ajax代码

[英]Select box to send to ajax code

I have been using a snippet of code to add data to a mysql database using ajax.I have this working perfectly using a text box to add, however I can't seem to get it working by replacing text box with select box. 我一直在使用一小段代码通过ajax将数据添加到mysql数据库中,我可以使用文本框添加来完美地完成此工作,但是我似乎无法通过将文本框替换为选择框来使其正常工作。

Here is the original text box: 这是原始文本框:

 <form class="add-new-task" autocomplete="off">
     <input type="text" name="new-task" placeholder="Add a new item..." />
    </form>

Here is the code I have replaced it with: 这是我替换为的代码:

<form class="add-new-task" autocomplete="off">
 <select name = "new-task">
    <option value="test1">test1</option>
    <option value="test2">test2</option>
 </select>
   <input type='submit' value='submit'/>
</form>

Here is the receiving code: 这是接收代码:

   <script src="http://code.jquery.com/jquery-latest.min.js"></script>
   function add_task() {
    $('.add-new-task').submit(function(){
    var new_task = $('.add-new-task input[name=new-task]').val();

    if(new_task != ''){
    $.post('add-task.php', { task: new_task }, function( data ) {
        $('.add-new-task input[name=new-task]').val('');
        $(data).appendTo('.task-list ul').hide().fadeIn();
                delete_task();
            });
    }
    return false; // Ensure that the form does not submit twice
    });
}

function delete_task() {
    $('.delete-button').click(function(){
    var current_element = $(this);
    var id = $(this).attr('id');

    $.post('delete-task.php', { task_id: id }, function() {
    current_element.parent().fadeOut("fast", function() { $(this).remove(); });
    });
    });
}

I thought it would be simple and it probably is but I can't get it working, Thanks in advance. 我认为这很简单,可能是这样,但是我无法正常工作,谢谢。

(Bill - Thanks for tidying it up) (帐单-感谢您整理)

As you replaced the input with select, below code needs to be changed 当您将输入替换为select时,需要更改以下代码

$('.add-new-task input[name=new-task]').val();

this should work 这应该工作

$('.add-new-task select[name=new-task]').val();

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

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