繁体   English   中英

选择框以发送到Ajax代码

[英]Select box to send to ajax code

我一直在使用一小段代码通过ajax将数据添加到mysql数据库中,我可以使用文本框添加来完美地完成此工作,但是我似乎无法通过将文本框替换为选择框来使其正常工作。

这是原始文本框:

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

这是我替换为的代码:

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

这是接收代码:

   <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(); });
    });
    });
}

我认为这很简单,可能是这样,但是我无法正常工作,谢谢。

(帐单-感谢您整理)

当您将输入替换为select时,需要更改以下代码

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

这应该工作

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

暂无
暂无

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

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