問題描述
我一直在為我的 web 應(yīng)用程序使用一個(gè)名為 DropKick 的漂亮、優(yōu)雅的插件 http://jamielottering.github.com/DropKick/,我似乎有一個(gè)小問題,不知道如何去嘗試修復(fù)它.我正在嘗試以編程方式更改選擇下拉菜單的值.下面是對(duì)我的問題的描述,以及 JSFiddle 的鏈接.
I've been using a nice, elegant plugin called DropKick for my webapp http://jamielottering.github.com/DropKick/, and I seem to be having a slight issue with it and am not sure how to go about trying to fix it. I am trying to programmatically change the value of the select drop down menu. Below is a description of my issue, and a link to JSFiddle.
HTML:
jQuery:
當(dāng)我在警報(bào)中顯示該值時(shí),它顯示為 1,但是當(dāng)我查看選項(xiàng)上的標(biāo)簽時(shí),它保持默認(rèn)值或更改之前的任何值.
When I show the value in alert, it shows as one, however when I look at the labels on the option it stays at the default or whatever it was prior to the change.
例如,如果我的默認(rèn)設(shè)置是Choose"并且我點(diǎn)擊了 someDiv,那么 alert 將顯示1",因此它會(huì)發(fā)生變化,但選擇下拉菜單仍會(huì)顯示Choose".有什么建議.我可能只是遺漏了一些小東西,不確定.
For example, if my default was "Choose" and I click someDiv, then alert will show "1", so it changing, but the select dropdown will still show "Choose". Any suggestions. I may just be missing something small, not sure.
FSFiddle:http://jsfiddle.net/kdp8791/aNS9R/61/
推薦答案
工作演示 : With Commnet http://jsfiddle.net/aNS9R/218/ &&無注釋只需要 7 行:http://jsfiddle.net/aNS9R/220/
working demo : With Commnet http://jsfiddle.net/aNS9R/218/ && without comments only 7 lines needed: http://jsfiddle.net/aNS9R/220/
-呼-
所以,首先,我嘗試使用 in drop kick 更改 [of dropkick] 事件,但它僅適用于 select 中的更改事件,不是來自外部元素綁定. 即在您的情況下更改按鈕.
So, to start with I tried Change event [of dropkick] with in drop kick but its only for the change event within select and not from external element binding. i.e. in your case change button.
所以;這就是我所做的:
So; this is what I have done:
說明(如果您有興趣)
我使用 firebug 檢查變量,發(fā)現(xiàn)當(dāng)你使用 $('#timePreference option:selected').val("1");
dropkick 實(shí)際上確實(shí)使用 id=timePreference 在您的元素中更改了所選值,但由 dropkick 創(chuàng)建的 div 和 ul 和 li 樣式
尚未更改.
I used firebug to inspect the variable and found that dropkick marshal your existing select with nice styling now when you used $('#timePreference option:selected').val("1");
dropkick actually did changed the selected value with in your element with id=timePreference but the div and ul and li styling
which is created by dropkick is not changed yet.
對(duì)于選擇的跨度,它有一個(gè)類 .dk_label
并且對(duì)于當(dāng)前(綠色)由 .dk_option_current
類給出.
For the chosen span it has a class .dk_label
and for the current (green color) is given by .dk_option_current
class.
請(qǐng)注意我?guī)缀蹰喿x了插件并從這里弄清楚發(fā)生了什么:https://github.com/JamieLottering/DropKick/blob/master/jquery.dropkick-1.0.0.js
Please Note I pretty much read the plugin and figure out what is happening from here: https://github.com/JamieLottering/DropKick/blob/master/jquery.dropkick-1.0.0.js
如果您想使用 firebug 并查看元素如何使用此鏈接:http://jsfiddle.net/aNS9R/218/show/ 并使用您的檢查模式,您將看到 dropkick 樣式及其工作原理.
If you wish to use firebug and see how elements are se use this link : http://jsfiddle.net/aNS9R/218/show/ and play around with your inspect mode, you will see dropkick styling and how it works.
JQuery 代碼
希望這可以幫助你交配,干杯!
Hope this helps you mate, cheers!
HTML
這篇關(guān)于使用 DropKick Javascript 設(shè)置值/標(biāo)簽的問題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!