第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jQuery函數(shù)中的PHP變量未正確傳遞

jQuery函數(shù)中的PHP變量未正確傳遞

紅糖糍粑 2022-01-07 19:09:00
我有幾個 jQuery 函數(shù),它們使用 wordpresswp_localize_scripts函數(shù)將 PHP 數(shù)據(jù)傳遞給 JS函數(shù):function mb_scripts_settings() {    // blanks    $mb_ajax_form_type = $mb_get_page_slug = $mb_redirect = $mb_redirect_time = $mb_form_disable = $mb_array = '';    // get the form type    $mb_ajax_form_type      = ( is_front_page() ? 'change'  : 'submit'  );    // get the page slug from ID    $mb_get_page_slug       = get_post_field( 'post_name', get_the_ID() );    // if the page is admin or password    if( is_page( array('admin', 'pw') ) ) {        $mb_redirect        = true;        $mb_redirect_time   = 3000;        $mb_form_disable    = true;        if( is_page('admin') ) {            // generate the url for redirection            $mb_form_area           = ( ( is_page('admin') && isset($_GET['mbtab']) )   ? $_GET['mbtab']    : null      );            $mb_form_area_url       = ( empty($mb_form_area)    ? '/' : '/admin/?mbtab=' . $mb_form_area . '&mbform=1'  );            $mb_form_area_url       = get_home_url( $mb_form_area_url );        }    }    // if the page is front    if( is_front_page() ) {        $mb_redirect        = false;        $mb_redirect_time   = 0;        $mb_form_disable    = false;        $mb_get_page_slug   = 'front_page';        $mb_form_area = $mb_form_area_url = null;    }    // build the array    $mb_array = array(                $mb_ajax_form_type,                $mb_get_page_slug,                $mb_redirect,                $mb_redirect_time,                $mb_form_disable            );    return $mb_array;}但是,當它沒有通過數(shù)組傳遞并且全部硬編碼時,它似乎并沒有像我想象的那樣mb_form_type工作。直到我用'.' + mb_form_type.現(xiàn)在它在complete:函數(shù)上吐出一個錯誤,但我也嘗試將 if 語句設置為比較為String()==String(mb_get_page_slug) == String('admin')但這也不起作用。在比較中我缺少什么嗎?
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經(jīng)驗 獲得超7個贊

您的問題是范圍之一。 $(function() {});創(chuàng)建一個閉包,您在該閉包中定義您的變量。閉包外的代碼看不到這些變量。要解決此問題,您有幾個選項,這里有 2 個可行:


1)像這樣將變量移動到閉包之外:


// var

var mb_form_type        = mb_mbtheme_js[0],

    mb_form_type        = '.' + mb_form_type,

    mb_get_page_slug    = mb_mbtheme_js[1],

    mb_redirect         = mb_mbtheme_js[2],

    mb_redirect_time    = mb_mbtheme_js[3],

    mb_form_disable     = mb_mbtheme_js[4];



// create the script

$(function() {


    // trigger the ajax on form type

    // $("#mb_ajax_form") + mb_form_type + ( function( mb_ajax_form ) {

    $("#mb_ajax_form").change( function( mb_ajax_form ) {


        // stop the default function of buttons

        mb_ajax_form.preventDefault();


        // do the ajax

        mb_ajax_form_js();


    });


});


// accept the form ID

function mb_ajax_form_js() {

    // your code here...omitted for brevity 

}

2)像這樣在閉包內(nèi)移動你的函數(shù)(注意,任何調(diào)用mb_ajax_form_js也需要在閉包內(nèi)):


// create the script

$(function() {


    // var

    var mb_form_type        = mb_mbtheme_js[0],

        mb_form_type        = '.' + mb_form_type,

        mb_get_page_slug    = mb_mbtheme_js[1],

        mb_redirect         = mb_mbtheme_js[2],

        mb_redirect_time    = mb_mbtheme_js[3],

        mb_form_disable     = mb_mbtheme_js[4];


    // trigger the ajax on form type

    // $("#mb_ajax_form") + mb_form_type + ( function( mb_ajax_form ) {

    $("#mb_ajax_form").change( function( mb_ajax_form ) {


        // stop the default function of buttons

        mb_ajax_form.preventDefault();


        // do the ajax

        mb_ajax_form_js();


    });




    // accept the form ID

    function mb_ajax_form_js() {

        // your code here...omitted for brevity 

    }


});

更新:

要使用字符串變量 ( )訪問submitandchange函數(shù)mb_form_type,您需要使用“數(shù)組訪問語法”而不是您嘗試過的點表示法。


作為一個簡單的示例,這將起作用(注意 mb_form_type 不包含.):


var mb_form_type = 'change';



$("#mb_ajax_form")[mb_form_type]( function( mb_ajax_form ) {

    alert('This will work using array access syntax');

});


查看完整回答
反對 回復 2022-01-07
  • 1 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號