【发布时间】:2017-01-31 11:21:40
【问题描述】:
我知道这是一个常见问题,我尝试了所有找到的解决方案。
无论如何,似乎没有任何效果。
我也尝试使用val()、text() 和html() 来获取值,正如有人建议的那样:NOTHING!
这是我的 HTML(我们在 Wordpress 中):
<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);
},
});*/
});
整个JS都在jQuery(document).ready(function($){}里面。
一开始以为是WP Ajax的问题,后来发现变量“task_content”其实根本没有贴出来,console里除了“task_due”值外什么也没有出现。
这是我看不到的错字吗?
我从我的应用程序的另一个页面克隆了 JS 和 HTML,它运行良好。我不明白问题出在哪里!
编辑:
HTML 包含在一个对话窗口中,这是一种类似于 Bootstrap 的模式。
所以我改变了这样的按钮:
<button type="button" onclick="createNewTask('<?php echo $_GET['id']; ?>');" id="new_task" class="button-helpy"><?php _e( 'Confirm', 'helpy'); ?></button>
还有JS函数:
function createNewTask(id) {
var task_due = jQuery('#new_task_due').val();
var task_content = jQuery('#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: id,
todo: 'task',
};
jQuery.ajax({
type: "POST",
url: ajaxurl,
data: data,
dataType: 'JSON',
success: function(response) {
jQuery('#response').fadeIn(300, function(){ jQuery(this).html(response.confirm); });
setTimeout(function() {
jQuery('#response').fadeOut(300, function(){ jQuery(this).empty();});
}, 5000);
},
});*/
}
结果是一样的:(
【问题讨论】:
-
控制台有js冲突吗?
-
什么都没有!
-
从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。
-
我真的不知道。这是一种让我头脑发热的错误! :)
-
@Banzay 也尝试了 on() 但没有
标签: jquery ajax wordpress textarea