【问题标题】:Select box to send to ajax code选择框发送到 ajax 代码
【发布时间】:2015-08-11 08:33:49
【问题描述】:

我一直在使用 sn-p 代码使用 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(); });
    });
    });
}

我认为这很简单,但我无法让它工作, 提前致谢。

(比尔 - 感谢整理)

【问题讨论】:

  • name = "new-task" -> name="new-task"

标签: ajax select drop-down-menu


【解决方案1】:

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

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

这应该可以工作

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

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多