简体   繁体   English

jQuery从selectbox中获取选定值不起作用

[英]jQuery get selected value from selectbox not working

I am working on a Woocommerce (WordPress) project and my task is to get selected value from drop-down list when user click on button. 我正在开发一个Woocommerce(WordPress)项目,我的任务是在用户点击按钮时从下拉列表中获取所选值。

My HTML is: 我的HTML是:

<select name="attribute_taxonomy" id="attribute_taxonomy" class="attribute_taxonomy">
    <option value="">Custom product attribute</option>
    <option value="pa_bedroom">Bedroom</option>
    <option value="pa_bathroom">Bathroom</option>
</select>

<button type="button" class="button button-primary add_attribute">Add</button>

and jQuery code is: 和jQuery代码是:

$('.add_attribute').on('click', function () {
    var selected = $('#attribute_taxonomy option:selected');
    alert(selected.val());
});

Unfortunately I am getting blank alert box, not getting anything. 不幸的是,我得到了空白警报框,没有得到任何东西。 But strange is when I create jsFiddle with same HTML & jQuery code I am getting right output. 但奇怪的是,当我用相同的HTML和jQuery代码创建jsFiddle时,我得到正确的输出。

Why I am not getting anything. 为什么我什么都没得到。 Is there any alternate solution? 有替代解决方案吗? I am not good with jQuery so I will thank-full if someone guide me to fix this issue. 我对jQuery不好,所以如果有人指导我解决这个问题,我会感激不尽。

My Sample > JSFIDDLE 我的示例> JSFIDDLE

Thanks. 谢谢。

Simply use: 只需使用:

var selected = $('#attribute_taxonomy');
alert( selected.val() );

You need to get the value of the select element, not its selected option : 您需要获取select元素的值,而不是其选定的option

$('.add_attribute').on('click', function () {
    var selected = $('#attribute_taxonomy');
    alert(selected.val());
});

JS Fiddle demo . JS小提琴演示

I'd also tend to prevent the user interacting with the button until a choice is made in the select element (using the disabled (Boolean) property) and then , once a choice is made, prevent the user from reselecting what appears to be an option label (note this is more complicated than it needs to be, a possibly improved approach will follow). 我也倾向于阻止用户与button交互,直到在select元素中select (使用disabled (布尔)属性), 然后 ,一旦做出选择,阻止用户重新选择看起来像是option标签(注意这比它需要的更复杂,可能会采用一种可能改进的方法)。 First, the HTML: 首先,HTML:

<!-- select element unchanged -->

<button type="button" class="button button-primary add_attribute" disabled>Add</button>

jQuery: jQuery的:

$('#attribute_taxonomy').on('change', function(){
    // cache the $(this) jQuery object since we're potentially using it twice:
    var $this = $(this);
    // if there are no disabled options we run the following jQuery:
    if (!$this.find('option:disabled').length) {
        $this
        // find the option that has no value set:
        .find('option[value=""]')
        // set its 'disabled' property to true (to disable it)
        .prop('disabled', true)
        // go back to the original selector ($(this))
        .end()
        // move to the next element (if it's a button element):
        .next()
        // unset its 'disabled' property (to enable it):
        .prop('disabled',false);
    }
});
$('.add_attribute').on('click', function () {
    var selected = $('#attribute_taxonomy');
    alert(selected.val());
});

JS Fiddle demo . JS小提琴演示

A slightly better approach (to my mind) is to use HTML, and its elements, properly; 一个稍微好一点的方法(在我看来)是正确使用HTML及其元素; using an optgroup to associate the relevant option elements, with a label attribute set to explain the contents of that group: 使用optgroup关联相关的option元素,并使用label属性设置来解释该组的内容:

<select name="attribute_taxonomy" id="attribute_taxonomy" class="attribute_taxonomy">
    <optgroup label="Custom product attribute">
    <option value="pa_bedroom">Bedroom</option>
    <option value="pa_bathroom">Bathroom</option>
    </optgroup>
</select>

JS Fiddle demo . JS小提琴演示

This approach means that an option, with a value, is always set (though initially to a default unless one of those option s is given a selected attribute), which means the button doesn't need to have its disabled property set/unset to prevent accidentally choosing an option without a value. 这种方法意味着始终设置带有值的选项(尽管最初为默认设置,除非其中一个option被赋予selected属性),这意味着该button不需要将其disabled属性设置/取消设置为防止意外选择没有值的option

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM