RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[ajax]

Martin Hope
Andy_bat
Asked: 2022-08-10 21:22:35 +0000 UTC

如何通过 Ajax 传递多个参数

  • 0

我无法克服,我转向社区。有一个模板,我在其中通过str_replace()插入必要的参数

$output = "<div class='row'><KEYONE></div>"; //шаблон
$one_key = '<div class="card text-bg-secondary" style="width: 10rem;margin: 10px;padding: 0">'
  .'<div class="card-header form_radio_btn" style="padding: 0;height: 40px;display: block"><span class="form_radio_btn" title="<KEYTITLE>" style="">'
  .'<input id="radio-<KEYNUM>" type="radio" name="radio" value="<HEXKEY>"><label for="radio-<KEYNUM>"><KEY></label></span></div>'
  .'<div class="card-body" style="display: table-row;margin: 0;padding: 0">'
  .'<div id="key-<KEYNUM>" style="width: 90px;height: 50px;float:left;text-align: center;margin: 2px 0 0 0;color: #cbcbcb"><i class="fa fa-key fa-3x"></i></div>'
  .'<button id="btnGroupVerticalDrop-<KEYNUM>" type="button" class="btn dropdown-toggle" data-bs-toggle="dropdown" style="float: right"></button>'
  .'<ul class="dropdown-menu" aria-labelledby="btnGroupVerticalDrop-<KEYNUM>" >'
  .'<li><a class="dropdown-item ajax-inc" data-global=\'{"ajroute":"task_info_key","key":"<KEYNUM>"}\'  href="#" >Информация по ключу</a></li>'
  .'<li><a class="dropdown-item ajax-inc" href="#">Сброс ключа</a></li>'
  .'</ul></div></div>';
$one_key = str_replace("<KEY>","123456",$one_key);
$one_key = str_replace("<KEYNUM>","123456",$one_key);
$one_key = str_replace("<HEXKEY>","abcdef",$one_key);
$output = str_replace("<KEYONE>",$one_key,$output);
echo $output;

一切正常,菜单扩展。我需要通过 Ajax 更新键列表,因为这个块通过按键数显示单个元素来“相乘”。我无法通过 Ajax “推动”复杂的数据全局构造(如果删除它,则一切正常)。我强调——这个块是通过替换来显示的。试图改变引号,逃避 - 没有帮助。

错误 - Uncaught SyntaxError: missing ) 在参数列表 Ajax 本身之后:

"$('#keylist').html('<div class=\"card text-bg-secondary\" style=\"width: 10rem;margin: 10px;padding: 0\"><div class=\"card-header form_radio_btn\" style=\"padding: 0;height: 40px;display: block\"><span class=\"form_radio_btn\" title=\"версия микропрограммы: 16778249\" style=\"\"><input id=\"radio-1065509103\" type=\"radio\" name=\"radio\" value=\"3f8260ef\"><label for=\"radio-1065509103\"><b>3f8260ef</b></label></span></div><div class=\"card-body\" style=\"display: table-row;margin: 0;padding: 0\"><div id=\"key-1065509103\" style=\"width: 90px;height: 50px;float:left;text-align: center;margin: 2px 0 0 0;color: #cbcbcb\"><i class=\"fa fa-key fa-3x\"></i></div><button id=\"btnGroupVerticalDrop-1065509103\" type=\"button\" class=\"btn dropdown-toggle\" data-bs-toggle=\"dropdown\" style=\"float: right\"></button><ul class=\"dropdown-menu\" aria-labelledby=\"btnGroupVerticalDrop-1065509103\" ><li><a class=\"dropdown-item ajax-inc\" data-global='{\"ajroute\":\"task_info_key\",\"key\":\"1065509103\"}'  href=\"#\" >Информация по ключу</a></li><li><a class=\"dropdown-item ajax-inc\" href=\"#\">Сброс ключа</a></li></ul></div></div><div class=\"card text-bg-secondary\" style=\"width: 10rem;margin: 10px;padding: 0\"><div class=\"card-header form_radio_btn\" style=\"padding: 0;height: 40px;display: block\"><span class=\"form_radio_btn\" title=\"версия микропрограммы: 16778245\" style=\"\"><input id=\"radio-1065510441\" type=\"radio\" name=\"radio\" value=\"3f826629\"><label for=\"radio-1065510441\"><b>3f826629</b></label></span></div><div class=\"card-body\" style=\"display: table-row;margin: 0;padding: 0\"><div id=\"key-1065510441\" style=\"width: 90px;height: 50px;float:left;text-align: center;margin: 2px 0 0 0;color: #cbcbcb\"><i class=\"fa fa-key fa-3x\"></i></div><button id=\"btnGroupVerticalDrop-1065510441\" type=\"button\" class=\"btn dropdown-toggle\" data-bs-toggle=\"dropdown\" style=\"float: right\"></button><ul class=\"dropdown-menu\" aria-labelledby=\"btnGroupVerticalDrop-1065510441\" ><li><a class=\"dropdown-item ajax-inc\" data-global='{\"ajroute\":\"task_info_key\",\"key\":\"1065510441\"}'  href=\"#\" >Информация по ключу</a></li><li><a class=\"dropdown-item ajax-inc\" href=\"#\">Сброс ключа</a></li></ul></div></div>')"
php ajax
  • 1 个回答
  • 30 Views
Martin Hope
Салават
Asked: 2022-08-10 21:04:19 +0000 UTC

为什么它只有效?

  • 0

Laravel 控制器中有这段代码:

public function put_in_favorites(Request $request)
$is_active = $request->get('is_active');      
    if ($is_active)
    {
       
            return 'this is true';
    }
    else
        {
            return 'this is false';
        }

它从 ajax 请求中获取$is_active的值:

$('.bpt-heart').on('click', function (){
        $(this).toggleClass('Active');
        let IsActive;
        if ($(this).hasClass('Active'))
        {
            IsActive = true;
        }
        else
        {
            IsActive = false;
        }
        dataId = this.getAttribute('data-id');
        // console.log(dataId);
    $.ajax({
        url: 'favorites',
        type: 'POST',
        headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
        data: {
            is_active: IsActive,
            data_id: dataId
        },
        success: function (data)
        {
            console.log(data);
        },
        error: function (err)
        {
            console.log('Error');
            console.log(err);
        }
    });
});

但是,这对我来说总是返回 true,尽管如果返回$is_active,它会根据需要更改为true和false。如何根据$is_active变量在控制器中处理这两种情况?

javascript ajax
  • 2 个回答
  • 42 Views
Martin Hope
Siparat
Asked: 2022-08-08 22:50:35 +0000 UTC

如何将 fetch 的响应覆盖到变量中?[复制]

  • 0
这个问题已经在这里得到了回答:
如何从事件或回调函数返回值?或者至少等待他们完成 3 个答案
1 个月前关闭。

const URL = 'https://jsonplaceholder.typicode.com/users'

const request = (method, url, body = null) =>{
    return fetch(url, {
        method: method,
        body: body
    }).then(request => {
        return request.json()
    })
}

a = request('GET', URL).then(data =>{
    return data
})

console.log(a )
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script src="fetch.js"></script>
</body>
</html>

javascript ajax
  • 1 个回答
  • 28 Views
Martin Hope
Sergiu
Asked: 2022-08-01 16:23:24 +0000 UTC

为什么如果标签是用 ajax 添加的,我无法用 JavaScript 读取它们

  • 0

我到处寻找这个解决方案,但找不到可以帮助我的答案。我的标签是用数据库中的 ajax 以这种方式呈现的代码:

function all() {
    // Ajax config
    $.ajax({
        type: "GET", //we are using GET method to get all record from the server
        url: 'all.php', // get the route value
        success: function (response) {//once the request successfully process to the server side it will return result here
            
            // Parse the json result
            response = JSON.parse(response);

            var html = "";
            // Check if there is available records
            if(response.length) {
                html += '<div class="list-group">';
                // Loop the parsed JSON
                $.each(response, function(key,value) {
                    // Our employee list template
                    html += '<a href="#" class="list-group-item list-group-item-action">'; 
                    html += "<p class='m-0'>" + value.let_txt_3 + "</p>";
                    html += "<p class='m-0'>" + value.let_txt_4 + "</p>";
                    html += "<p class='m-0'>" + value.let_txt_5 + "</p>";  
                    html += "<p class='m-0'>" + value.let_txt_6 + "</p>";  
                    html += "<button type='button' class='btn btn-primary' data-bs-toggle='modal' data-bs-target='#edit-employee-modal' id='edit_employee_modal' data-bs-id='"+value.id+"'>Edit</button>";
                    html += '</a>';
                });
                html += '</div>';
            } else {
                html += '<div class="alert alert-warning">';
                  html += 'No records found!';
                html += '</div>';
            }

            

            // Insert the HTML Template and display all employee records
            $("#employees-list").html(html);
        }
    });
}

我想操作 html 按钮,但我不能。

html += "<button type='button' class='btn btn-primary' data-bs-toggle='modal' data-bs-target='#edit-employee-modal' id='edit_employee_modal' data-bs-id='"+value.id+"'>Edit</button>";

例如,帮助我执行以下操作:

  • 当您按下按钮显示alert("test");
  • 或者edit_employee_modal.style.color = "red";

我的问题是我不知道这个错误,inspect-> console(点击按钮):

modal.js:332 Uncaught TypeError: Cannot read properties of undefined (reading 'classList')
at De._isAnimated (modal.js:332:26)
at De._initializeBackDrop (modal.js:205:24)
at new De (modal.js:82:27)
at De.getOrCreateInstance (base-component.js:55:41)
at HTMLButtonElement.<anonymous> (modal.js:434:22)
at HTMLDocument.n (event-handler.js:120:21)

我试过什么?!

let edit_employee_modal = document.querySelector("#edit_employee_modal");
edit_employee_modal.onclick = function() {
    edit_employee_modal.style.color = "red"; 
}

页面加载后尝试执行脚本:

window.document.onload = function(e){ 
let edit_employee_modal = document.querySelector("#edit_employee_modal");
    edit_employee_modal.onclick = function() {
        edit_employee_modal.style.color = "red"; 
    }
}

我用下面的代码替换了它,它也不起作用(因为我认为 javascript 无法读取我的标签):

window.onload = function(e){ 
   //code 
}
document.addEventListener("DOMContentLoaded", function(event) { 
  // code 
});

你能帮我出主意吗?

ps:我是新手,不要评价太苛刻

javascript ajax
  • 1 个回答
  • 68 Views
Martin Hope
Михаил Горячев
Asked: 2022-07-30 23:17:59 +0000 UTC

使用一个 ajax 脚本发送 2 个表单

  • 1

请告诉我如何正确编写 Ajax 脚本,以便在提交第二个表单时页面不会重新加载。有必要恰好保留 2 个表格。

代码中发生了什么:add_review通过 ajax 提交第一个表单时,我成功提交了第二个表单$('.rating').submit();,但在这种情况下页面会重新加载。是否可以添加脚本以便发送第二个表单但不重新加载页面。

设计:

<form class="rating" method="POST" action="updates/add_review_stars.php" enctype="multipart/form-data">
    <input type="hidden" class="uk-input" value="<?echo $product_id;?>" name="product_id" id="product_id">
    <label>
    <input type="radio" name="stars" value="1" />
    <span class="icon">★</span>
    </label>
    <label>
    <input type="radio" name="stars" value="2" />
    <span class="icon">★</span>
    <span class="icon">★</span>
    </label>
    <label>
    <input type="radio" name="stars" value="3" />
    <span class="icon">★</span>
    <span class="icon">★</span>
    <span class="icon">★</span>   
    </label>
    <label>
    <input type="radio" name="stars" value="4" />
    <span class="icon">★</span>
    <span class="icon">★</span>
    <span class="icon">★</span>
    <span class="icon">★</span>
    </label>
    <label>
    <input type="radio" name="stars" value="5" />
    <span class="icon">★</span>
    <span class="icon">★</span>
    <span class="icon">★</span>
    <span class="icon">★</span>
    <span class="icon">★</span>
    </label>
    </form>

    <form id ="add_review" enctype="multipart/form-data">
    <input type="hidden" class="uk-input" value="<?echo $product_id;?>" name="product_id" id="product_id">
    <input type="hidden" class="uk-input" value="0" name="approved" id="approved">
    </br>
    <label class="uk-form-label" for="form-stacked-text">Имя</label>
    <input type="text" class="uk-input" name="name" id="name" required>
    <label class="uk-form-label" for="form-stacked-text">Отзыв</label>
    <textarea class="uk-textarea" name="review" id="review" required></textarea>
    <div class="uk-margin">
    <input type="submit" name="submit" value="Добавить отзыв" data-dismiss="modal" class="uk-button uk-button-default">
    </div>
    </form>

    <script>
    $("#add_review").submit(function(send){
    send.preventDefault();
    $.ajax({
        url: 'updates/add_review.php',
        type: 'POST',
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        success: function(html){  
            $("#addresult_review").html(html);
            $('.rating').submit();
        }  
    });
    });
    </script>
php ajax
  • 1 个回答
  • 27 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5