dongwuwu6104 2015-12-31 04:52
浏览 6
已采纳

Wordpress PHP帮助

A recent update to woocommerce has broken a simple plugin that was created. I'm a bigtime rookie when it comes to referencing HTML elements with PHP code.

The plugin simply adds the data that is in the variable "hiddenRef" to the element on a particular page, and makes the input box read only.

This is the code:

jQuery(document).ready(function($) {
    var hiddenRef = $("#hiddenRef").val();  
    $("input[name*='poster[reference]']").val(hiddenRef);
    if(hiddenRef !=""){
        $("input[name*='poster[reference]']").attr("readOnly", "readOnly");     
    }
}

The page that woocommerce updated now shows the field to be:

<input type="text" class="input-text addon addon-custom" data-price="" name="addon-707-poster-0[reference]" value="">

Can someone please help me tweak the code above to add the value "hiddenRef" to the value section of the HTML element. I'm not sure what name*='poster[reference] is looking for.

  • 写回答

1条回答 默认 最新

  • drvlf9739 2015-12-31 05:14
    关注

    The code is not working because the input field's name has been changed.

    Either you need to use the new name or you need to add a custom id or class to the input element.

    jQuery(document).ready(function($) {
    
            var hiddenRef = $("#hiddenRef").val();
            $("input[name*='addon-707-poster-0[reference]']").val(hiddenRef);
    
            if (hiddenRef != "") {
                $("input[name*='addon-707-poster-0[reference]']").attr('readOnly', true);
            }
        )
    };
    

    You can also use class to make the field read-only.

    jQuery(document).ready(function($) {
    
            var hiddenRef = $("#hiddenRef").val();
            $(".addon.addon-custom").val(hiddenRef);
    
            if (hiddenRef != "") {
                $(".addon.addon-custom").attr('readOnly', true);
            }
        )
    };
    

    This will work if the input name is not dynamic. If the input is dynamic you need to inherit the woocommerce template and need to add a custom class or id to the input element.

    I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致