如何通过JavaScript更改所选属性 [英] How to change selected attribute via javascript

查看:124
本文介绍了如何通过JavaScript更改所选属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要解决的问题.需要一些正确方向的建议:

I have an issue that I'm trying to solve. Need some advice to the right direction:

使用Prestashop创建组合时,您会在产品页面上找到一个下拉列表,客户可以从中选择组合.在product.tpl文件中的此处,我创建了一些额外的按钮,以吸引客户注意某种组合.现在,我希望在用户单击该按钮时自动选择并激活此组合.我已经在product.tpl文件中输入了一些javascript代码,当单击按钮时,该代码也隐藏了其他一些元素.我只是没有让它起作用,那就是在mouseclick上选择了某个属性选项.有人可能有解决这个问题的线索?

With Prestashop when creating combinations, you have a dropdwon-list on product page where customer can choose the combination from. Here within the product.tpl file I have created some extra button to get customers attention for a certain combination. Now I would like this combination beeing automatically selected and activated when user clicks on that button. I have inputted some javascript code within the product.tpl file which is also hiding some other elements when button is clicked. I just dont get it to work that a certain attribute option is selected on mouseclick. Someone may have some clue to solve this??

这是产品页面内属性的选择选项列表.例如,我想在按钮上单击选择选项大",并在模板中通过jquery/javascript的帮助通过onclick函数在产品页面中激活beeing.如何实现呢?

This is the select option list of the attributes within product page. For example I want on button click choose option "Large" and beeing activated within product page via onclick function with help of jquery/javascript within the template. How to achieve this?

<select id="group_4" class="attribute_select selectBox" onchange="findCombination();getProductAttribute();$('#wrapResetImages').show('slow');;" name="group_4" style="display: none;">
<option title="Small" selected="selected" value="21">Small</option>
<option title="Medium" value="41">Medium</option>
<option title="Large" value="40">Large</option>
<option title="Extralarge" value="25">Extralarge</option>

我已经尝试过了,但是没有用:

I have tried with this but it doesn't work:

$('#group_4 option[value='58']').attr('selected', 'selected').change();

推荐答案

要更改所选选项,只需使用纯JS更改select元素的值即可:

To change the selected option, you can simply change the value of the select element using plain JS:

document.getElementById('group_4').value = 58;

不过,在那之后,您仍然需要触发更改事件.

You'd still need to trigger the change event after that, though.

在此处查看简单示例: http://jsfiddle.net/BgBGL/1/(使用警报作为onchange回调)

See a simple example here: http://jsfiddle.net/BgBGL/1/ (uses alert as onchange callback)

这篇关于如何通过JavaScript更改所选属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆