在 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 值并将您重定向到该页面。

祝你好运;)