Javascript 访问WooCommerce产品页面中的产品属性选定值

Javascript 访问WooCommerce产品页面中的产品属性选定值,javascript,php,jquery,wordpress,woocommerce,Javascript,Php,Jquery,Wordpress,Woocommerce,我正在建一家商店,里面有各种各样的产品。我希望根据用户对select元素(它是产品的一个属性)的选择来隐藏其他元素 我编写了一些JavaScript代码,添加到functions.php文件中,以便在用户更改元素值时访问select元素的值。事件会正确触发,但在访问第一个元素时,即使选择了其他选项,我也会始终获取该元素的索引或值 这是functions.php中的代码: function set_select_event() { global $post;

我正在建一家商店,里面有各种各样的产品。我希望根据用户对select元素(它是产品的一个属性)的选择来隐藏其他元素

我编写了一些JavaScript代码,添加到functions.php文件中,以便在用户更改元素值时访问select元素的值。事件会正确触发,但在访问第一个元素时,即使选择了其他选项,我也会始终获取该元素的索引或值

这是functions.php中的代码:

    function set_select_event()
    {
        global $post;
        $product = wc_get_product($post->ID);
        if ($product->get_id() == "1365") {
        ?>
        <script>
        jQuery(document).ready(function($) {
            $('input.variation_id').change( function(){
                if( '' != $('input.variation_id').val() ) {
                    var var_id = $('input.variation_id').val();
                    var sel = document.getElementById("numerado");
                    var opcion = sel.value;
                    alert('You just selected variation #' + opcion);

                }
             });
        });
        </script>
        <?php
        }
    }
我也试过这样做:

add_action('woocommerce_single_product_summary', 'set_select_event');
    function set_select_event()
    {
        global $post;
        $product = wc_get_product($post->ID);
        if ($product->get_id() == "1365") {
        ?>
        <script>
        jQuery(document).ready(function($) {
            // Get the live selected value
            $('select#numerado').blur( function(){
                selectedValue = $('select#numerado option:checked').val();

                // Display on browser console (for test)
                console.log(selectedValue);
            });
        });
        </script>
        <?php
        }
    }
功能集\u选择\u事件()
{
全球$员额;
$product=wc\U get\U product($post->ID);
如果($product->get_id()=“1365”){
?>
jQuery(文档).ready(函数($){
//获取实时选定值
$('select#numerado').blur(函数(){
selectedValue=$('select#numerado option:checked').val();
//在浏览器控制台上显示(用于测试)
console.log(selectedValue);
});
});

您非常接近…要获取“numerado”属性下拉列表的选定值,请尝试:

add_action('woocommerce_single_product_summary', 'get_selected_product_attr_value');
function get_selected_product_attr_value()
{
    if ( get_the_id() == 1365 ) :
    ?>
    <script>
    jQuery(function($) {
        var a = 'select#numerado', 
            b = $(a).val(); // Get default selected value

        // Display the default selected value if not empty
        if( b != '' )
            console.log(b);

        // Get the live selected value
        $(a).blur( function(){
            b = $(this).val(); // Update default selected value

            // Display selected value on browser console (for test) if not empty
            if( b != '' )
                console.log(b);
        });
    });
    </script>
    <?php
    endif;
}
add_action('woocommerce_single_product_summary'、'get_selected_product_attr_value');
函数获取所选产品属性值()
{
如果(获取\u id()==1365):
?>
jQuery(函数($){
变量a='选择#数值',
b=$(a).val();//获取默认选定值
//如果不为空,则显示默认选定值
如果(b!='')
控制台日志(b);
//获取实时选定值
$(a).blur(函数(){
b=$(this).val();//更新默认选定值
//如果不为空,则在浏览器控制台(用于测试)上显示所选值
如果(b!='')
控制台日志(b);
});
});

谢谢,它终于开始工作了。我可以问一下我的代码有什么问题吗?我对jquery很迷茫。为什么$('select#numerado option:checked').val()没有得到值?@Antonio…主要问题是
option:checked')
selectedValue=$('select#numerado option:checked')。val();
而不仅仅是
selectedValue=$('select#numerado').val();
…现在如果您喜欢/想要,也可以请您回答这个问题,谢谢您。
add_action('woocommerce_single_product_summary', 'get_selected_product_attr_value');
function get_selected_product_attr_value()
{
    if ( get_the_id() == 1365 ) :
    ?>
    <script>
    jQuery(function($) {
        var a = 'select#numerado', 
            b = $(a).val(); // Get default selected value

        // Display the default selected value if not empty
        if( b != '' )
            console.log(b);

        // Get the live selected value
        $(a).blur( function(){
            b = $(this).val(); // Update default selected value

            // Display selected value on browser console (for test) if not empty
            if( b != '' )
                console.log(b);
        });
    });
    </script>
    <?php
    endif;
}