JQuery无法获得textarea的价值

时间:2017-01-31 11:21:40

标签: jquery ajax wordpress textarea

我知道这是一个常见的问题,我尝试了所有找到的解决方案。

无论如何,似乎没有任何工作。

我尝试使用.chello-worldCONTIKI_PROJECT = hello-world all: $(CONTIKI_PROJECT) CONTIKI = ../.. include $(CONTIKI)/Makefile.include 来获取值,正如有人建议的那样:没有!

这是我的HTML(我们在Wordpress中):

val()

和JS:

text()

整个JS包含html()

一开始,我认为这是WP Ajax的问题​​,但后来我看到变量" task_content"实际上根本没有发布,控制台中没有任何内容,除了" task_due"值。

这是我能看到的任何错字吗?

我从我的应用程序的另一个页面克隆了JS和HTML,这非常有效。我无法理解问题所在!

修改

HTML包含在一个对话窗口中,一个类似于Bootstrap的模式。

所以我改变了这样的按钮:

<div class="row">
    <div class="three columns">
        <?php _e( 'Content:', 'helpy'); ?>
    </div>
    <div class="nine columns">
        <textarea id="new_task_content" class="helpy-textarea" placeholder="<?php _e( 'Place the content here...', 'helpy'); ?>"></textarea>
    </div>
</div>
<div class="row">
    <div class="three columns">
        <?php _e( 'Due date:', 'helpy'); ?>
    </div>
    <div class="nine columns">
        <input type="date" id="new_task_due" class="helpy-date-input" value="<?php echo date('Y-m-d'); ?>">
    </div>
</div>
<div class="row">
    <div class="twelve columns">
        <button type="button" id="new_task" class="button-helpy"><?php _e( 'Confirm', 'helpy'); ?></button>
    </div>
</div>

JS功能:

$('#new_task').live('click', function() {
    var task_due = $('#new_task_due').val();
    var task_content = $('#new_task_content').val();
    console.log(task_content);
    console.log(task_due);
    /* var data = {
        action: 'project_request',
        task_content: task_content,
        task_due: task_due,
        id: '<?php echo $_GET['id']; ?>',
        todo: 'task',
    };

    $.ajax({
        type: "POST",
        url: ajaxurl,
        data: data,
        dataType: 'JSON',
        success: function(response) {
            $('#response').fadeIn(300, function(){ $(this).html(response.confirm); });
            setTimeout(function() {
                $('#response').fadeOut(300, function(){ $(this).empty();});
            }, 5000);
        },
    });*/
});

}

结果是一样的:(

3 个答案:

答案 0 :(得分:1)

我不知道你失败了什么,这里的小提琴正常工作:https://jsfiddle.net/py5scvhm/

BTW代替.live()使用.on().live()已被弃用http://api.jquery.com/live/

答案 1 :(得分:0)

好吧,在尝试了几乎所有内容之后,我决定将textarea插入一个bootstrap模式中。

不知何故,旧对话框的js代码阻止了值读取。我没有弄明白为什么以及它的哪一部分,但问题肯定存在。

感谢所有人帮助我!

答案 2 :(得分:-1)

而不是

$('#new_task_content').val();

尝试使用

获取价值
$('textarea#new_task_content').val();

同时在console&amp; amp;检查是什么对象

$('#new_task_content')