Wordpress PHP帮助

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.

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.