2017-02-04 91 views
0

我在spotfire的文本區域中有兩個計算值。我想使用jQuery來根據一個值是否高於另一個值來更改文本區域背景的顏色。我設置了它,但它不起作用。它看起來好像還沒有執行。這是HTML。使用jQuery更改文本區域的背景顏色Spotfire

<body id = wrapper> 
<SPAN id = thisyear><SpotfireControl id="2f97a6afc3e64512977dd042a7e32351"  /></SPAN> 

<SPAN id = lastyear ><SpotfireControl id="f98415c74eb34cedbab057f763788bc6" /></SPAN> 
</body> 

頂部計算值有(以ID thisyear)的值77750和底部的計算值有(以ID lastyear)44086

的想法是,當過濾器更改值我想要的背景顏色改變。這裏是目前不工作的jQuery:

$("#thisyear").change(function() { 
    var thisyearval = ParseInt($("#thisyear").val()); 
    var lastyearval = ParseInt($("#lastyear").val()); 
    if (thisyearval > lastyearval){ 
     $("#wrapper").css("background-color", "#009900") 
     } else{$("#wrapper").css("background-color", "#FF0000")} 
}); 

我是jQuery的新手,所以非常感謝任何幫助!

+2

我對Spotfire並不熟悉,但是您使用的'.change()'和'.val()'方法通常與'input'元素相關聯。我不會想象將它們附加到''標籤可以做任何事情。如果Spotfire生成單個輸入字段,請嘗試以這些字段爲目標,例如$('#thisyear input')'。 –

回答

1

我最終弄明白了這一點。下面是HTML:

<body > 
<div id = wrapper> 
<div id = thisyear><SpotfireControl id="d644de4c97c440fbb78c561f190e5a47" /> </div> 

<div id = lastyear ><SpotfireControl id="f98415c74eb34cedbab057f763788bc6" /></div> 
</div> 
</body> 

而jQuery的應該得到這個做:

setInterval(function() { 
    var thisyearval = parseInt($("#thisyear").text(),10) 
    var lastyearval = parseInt($("#lastyear").text(),10) 


    if (thisyearval > lastyearval){ 
     $("#wrapper").css("background-color", "#009900") 
    } else{$("#wrapper").css("background-color", "#FF0000")} 
}, 500); 

事實證明,Spotfire中犯規支持jQuery的變化的功能,所以我用的setInterval()基本上調用功能一遍又一遍。

希望這也能幫助別人。

+2

請注意''setInterval()',因爲它在Spotfire中沒有得到很好的支持。 'setInterval()'可以執行多次,導致分析中的競爭條件和奇怪的行爲。我還沒有嘗試過,但是您可以使用'clearInterval()'清理頁面導航上的定時器。有關提示,請參閱http://www.w3schools.com/jsref/met_win_setinterval.asp – niko

+0

感謝您的提示,我會研究它。 –