简体   繁体   English

Jquery 使用选择值更改脚本标记的 src

[英]Jquery to change src of a script tag using a select value

I need to change the value of the src in the tag :我需要更改标签中 src 的值

<script type='text/javascript' src='gray.js'></script>

For this I'm using a combobox:为此,我使用了一个组合框:

<select id="chartTheme">
   <option value="dark-blue">Dark Blue</option>
   <option value="gray">Gray</option>
</select>

How to make it use a Jquery function when I select and click in a refresh button?当我选择并单击刷新按钮时,如何使其使用 Jquery 函数?

Source to refresh button:刷新按钮的来源:

<input type="image" name="commit" src="../icons/refresh.png" onClick="javascript:updateGraph()"></input>

You could use $.getScript() for this purpose. $.getScript()您可以使用$.getScript() It loads a new script into the page and runs the code inside.它将新脚本加载到页面中并运行其中的代码。

function updateGraph()
{
    var sel = $('#chartTheme').val();

    $.getScript(sel + '.js');
}

Try this:尝试这个:

$('#chartTheme').change(function () {
    var optionVal = $(this).val();
    console.log(optionVal);
    updateGraph();
});

I think this should be like this:我认为这应该是这样的:

$('input[name="commit"]').click(function(){
    var srcExt = $('#chartTheme').val();
    $('<script'+' type="text/javascript"'+' src='+ srcExt +'></'+'script>').appendTo('body');
});
$('#chartTheme').change(function () {
    var optionVal = $(this).val();
    var miSrc = "someSrc";
    $('input.someClass').attr('src', miSrc);
    updateGraph();
});

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

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