问题
i am trying to make a cript which displays a embeded code of yahoo graph
embeded script is
<embed bgcolor="#dbdbd3" flashvars="lcId=1169793726234&state=symbol%3D^nsebank;range=1d;indicator=ema(13,34,55)+macd+rsi+stochasticfast;charttype=candlestick;crosshair=on;ohlcvalues=0;logscale=on;source=undefined" loop="false" menu="false" name="BANKNIFTY" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://us.js2.yimg.com/us.yimg.com/i/us/fi/yfc/swf/flashchart_1.18.swf" style="height: 775px; width: 550px" type="application/x-shockwave-flash" wmode="opaque"></embed>
in this script i want the value of state=symbol=^nsebank (^nsebank) to be taken from a drop down menu list and then when click on calculate button it displays the embeded code
i have written the code as follows -
<div id="looknorth"></div>
<script type="text/javascript">
function ln(){
D11=parseFloat(D1.value);
document.getElementById('looknorth').innerHTML='<embed bgcolor="#dbdbd3" flashvars="lcId=1169793726234&state=symbol%3D"+D11+";range=1d;indicator=ema(13,34,55)+macd+rsi+stochasticfast;charttype=candlestick;crosshair=on;ohlcvalues=0;logscale=on;source=undefined" loop="false" menu="false" name="BANKNIFTY" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://us.js2.yimg.com/us.yimg.com/i/us/fi/yfc/swf/flashchart_1.18.swf" style="height: 775px; width: 550px" type="application/x-shockwave-flash" wmode="opaque"></embed>';} </script>
<table border=1 cellpadding=2 cellspacing=1 width="400">
<tr>
<td style="background-color:#123742; font-size:12 ; font-weight:bold">
<font color="#FFFFFF">Compounding</font>
<td> <br />
<select name="D1">
<option value="^nsei">Annually</option>
<option value="^bsen">Half Yearly</option>
</select>
<tr>
<td colspan="2" ALIGN="center">
<input type=button style="background-color:#123742; color:#FFFFFF; font-weight:bold; font-size:15" name=calculate value=Calculate onClick=ln();>
</table>
The script is not working please help me in fixing the error.
回答1:
tfw.debugger:
- @parseFloat(D1)—string (the option value) doesn't provide a Number that can be parsed. The result that ends up in your embed tag is “NaN” and not the provided option value; The value provided in the second option didn't work for me, the first one did, so I would check on what you're sending in there.
- I noticed a '%3D' entity in your string which unescapes to an equals symbol. Check the syntax of the variable you're sending in. For example
state=symbol%3D'+D11+';range=1d;…
is what you have.- This translates to
state=symbol=^nsei;…
which looks very wrong. You might want to check those variables.
- This translates to
- error in string “innerHTML” @" – where you inserted D1 in the string.
Use the web-browser's console.log(string) console/debugger feature to test your objects and variables when in doubt (F12 to bring up the console/dev-tools in most browsers)
<div id="looknorth"></div>
<script language='javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type="text/javascript">
function ln(){
//var D11=String($('select[name=D1]').val());
var D11=String(D1.value),
text = '<embed bgcolor="#dbdbd3" flashvars="lcId=1169793726234&state=symbol%3D'+D11+';range=1d;indicator=ema(13,34,55)+macd+rsi+stochasticfast;charttype=candlestick;crosshair=on;ohlcvalues=0;logscale=on;source=undefined" loop="false" menu="false" name="BANKNIFTY" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://us.js2.yimg.com/us.yimg.com/i/us/fi/yfc/swf/flashchart_1.18.swf" style="height: 775px; width: 550px" type="application/x-shockwave-flash" wmode="opaque"></embed>';
//try { console.log(D11); } catch(err) { window.alert(String(D11)); }
//$('div#looknorth').html(text);
document.getElementById('looknorth').innerHTML=text;
}
</script>
<table border=1 cellpadding=2 cellspacing=1 width="400">
<tr>
<td style="background-color:#123742; font-size:12 ; font-weight:bold">
<font color="#FFFFFF">Compounding</font>
<td> <br />
<select name="D1" id="D1">
<option value="^nsei">Annually</option>
<option value="^bsen">Half Yearly</option>
</select>
<tr>
<td colspan="2" ALIGN="center">
<input type=button style="background-color:#123742; color:#FFFFFF; font-weight:bold; font-size:15" name=calculate value=Calculate onClick=ln();>
</table>
;)
回答2:
<div id="looknorth"></div>
<script type="text/javascript">
function ln(){
D1 = document.getElementsByName("D1")[0]
D11=parseFloat(D1.value);
document.getElementById('looknorth').innerHTML='<embed bgcolor="#dbdbd3" flashvars="lcId=1169793726234&state=symbol%3D'+D11+';range=1d;indicator=ema(13,34,55)+macd+rsi+stochasticfast;charttype=candlestick;crosshair=on;ohlcvalues=0;logscale=on;source=undefined" loop="false" menu="false" name="BANKNIFTY" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://us.js2.yimg.com/us.yimg.com/i/us/fi/yfc/swf/flashchart_1.18.swf" style="height: 775px; width: 550px" type="application/x-shockwave-flash" wmode="opaque"></embed>';
}
</script>
<table border=1 cellpadding=2 cellspacing=1 width="400">
<tr>
<td style="background-color:#123742; font-size:12 ; font-weight:bold">
<font color="#FFFFFF">Compounding</font>
<td> <br />
<select name="D1">
<option value="^nsei">Annually</option>
<option value="^bsen">Half Yearly</option>
</select>
<tr>
<td colspan="2" ALIGN="center">
<input type=button style="background-color:#123742; color:#FFFFFF; font-weight:bold; font-size:15" name="calculate" value="Calculate" onClick='ln()'>
You had some mismatched quotes around the innerHTML line. As well, you never defined what D1 was in your javascript so when you asked for D1.value, it didn't know what you wanted.
回答3:
There are few problems in your code:
- JS does not know that D1 is the name of your html select control unless you define that.
- Improper usage of single and double quotes while setting the inner html of looknorth.
- parseFloat expects a string that contains numeric values but you pass ^nsei for instance, which is invalid.
Replace JS function with this:
function ln(){
var D1 = document.getElementsByTagName("D1")[0];
var D11 = D1.value;
var lknorth = document.getElementById('looknorth');
lknorth.innerHTML= '<embed bgcolor="#dbdbd3" flashvars="lcId=1169793726234&state=symbol%3D' + D11 + ';range=1d;indicator=ema(13,34,55)+macd+rsi+stochasticfast;charttype=candlestick;crosshair=on;ohlcvalues=0;logscale=on;source=undefined" loop="false" menu="false" name="BANKNIFTY" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://us.js2.yimg.com/us.yimg.com/i/us/fi/yfc/swf/flashchart_1.18.swf" style="height: 775px; width: 550px" type="application/x-shockwave-flash" wmode="opaque"></embed>';
}
来源:https://stackoverflow.com/questions/10788296/adding-variable-in-embeded-code-with-dropdownlist