繁体   English   中英

在CodeIgniter中单击按钮时出现正则表达式错误的无效标志

[英]Invalid flag to regex error on button click in CodeIgniter

我有一个视图,其中使用外部js文件显示测验。 测验完成后,我将在屏幕上追加一些新的html,其中包括一个新按钮,该按钮应该被单击后将用户发送到我的主控制器的PracticeTask函数。 但是,单击它时出现错误:

Uncaught SyntaxError: Invalid flags supplied to RegExp constructor 'practiceTask'

由于代码位于.js文件中,据我所知,我不能使用site_url或base_url。 这是正确的方法吗?

相关JS:

$('#imageLocation').attr("src", "");
var html = '<div class="instruction_block"><p class="instruction_text" style="text-align: center; margin-top: 0">You have completed the ' + text + ' test</p><div class="button_placement" style="margin-top: 300px;"><input class="rounded" style="position: absolute; bottom: 0; right: 0; width: 250px;"" type="button" value="Continue to the next task" onClick="/main/practiceTask/3"></div>';
$('#final_message').append(html);

主控制器功能:

public function practiceTask($task_id){

$this->load->model('Main_model');

echo ($task_id);

$json_key = $this->Main_model->getKey($task_id);
$json_key = json_decode($json_key, true);

shuffle($json_key);

$data['test_key'] = $json_key;
$data['task_id'] = $task_id;

$this->load->view('practice_test_view', $data);

}

任何帮助将非常感激!

如果要在JS页面中获取值,可以将其放入隐藏的输入中,并在页面加载时与JS一起解析,以便您可以使用它。

您可能遇到的问题是onclick

onClick="/main/practiceTask/3"

用jQuery做到这一点

var html = '<div class="instruction_block"><p class="instruction_text" style="text-align: center; margin-top: 0">You have completed the ' + text + ' test</p><div class="button_placement" style="margin-top: 300px;"><input class="rounded" style="position: absolute; bottom: 0; right: 0; width: 250px;"" type="button" value="Continue to the next task" data-url="/main/practiceTask/3"></div>';
$(document).on('click', '.button_placement input', function(e){
    e.preventDefault(); 
    window.top.location = $(this).data('url');
 });

这样,它将创建一个事件,该事件将在您单击按钮时触发。 它将读取data-url值并将您重定向到该页面。

祝好运 ;)

暂无
暂无

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

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