Javascript JQuery设置要在标题前打印的下拉菜单值的全局变量
我正在尝试获取下拉菜单的值,并将其打印在部分标题之前。为了做到这一点,我需要检测下拉列表的变化,抓取所选文本,并将其打印到一个全局变量Javascript JQuery设置要在标题前打印的下拉菜单值的全局变量,javascript,jquery,global-variables,Javascript,Jquery,Global Variables,我正在尝试获取下拉菜单的值,并将其打印在部分标题之前。为了做到这一点,我需要检测下拉列表的变化,抓取所选文本,并将其打印到一个全局变量 jQuery(document).on('change', '#select_540', function(e) { globalvar disMetalType = console.log(this.options[e.target.selectedIndex].text); } 在我插入的地方,我有这个 <h1 id="custom_product_
jQuery(document).on('change', '#select_540', function(e) {
globalvar disMetalType = console.log(this.options[e.target.selectedIndex].text);
}
在我插入的地方,我有这个
<h1 id="custom_product_name">
<script type="text/javascript">
document.write (disMetalType);
</script>
<?php echo $_product->getNameCustom(); ?></h1>
document.write(disMetalType);
我需要一些帮助。我已经就同一个问题问了以前的问题,但做了一些研究,并尝试了不同的攻击角度。似乎应该使用下面的内容而不是文档。编写
jQuery('#custom_product_name').text(disMetalType);
似乎应该使用以下内容而不是document.write
jQuery('#custom_product_name').text(disMetalType);
您希望将插入作为更改时触发的代码的一部分。document.write仅在文件加载后被调用,此后不再调用 在你的例子中:
jQuery(document).on('change', '#select_540', function(e) {
globalvar disMetalType = console.log(this.options[e.target.selectedIndex].text);
//find the element you are trying to change:
$('#custom_product_name').prepend(this.options[e.target.selectedIndex].text + ' ');
}
为了避免在有人更改下拉列表并让其重复添加值时出现问题,我建议将html更改为:
<h1 id="custom_product_name">
<span id="placeholder"></span> <?php echo $_product->getNameCustom(); ?></h1>
您希望将插入作为更改时触发的代码的一部分。document.write仅在文件加载后被调用,此后不再调用 在你的例子中:
jQuery(document).on('change', '#select_540', function(e) {
globalvar disMetalType = console.log(this.options[e.target.selectedIndex].text);
//find the element you are trying to change:
$('#custom_product_name').prepend(this.options[e.target.selectedIndex].text + ' ');
}
为了避免在有人更改下拉列表并让其重复添加值时出现问题,我建议将html更改为:
<h1 id="custom_product_name">
<span id="placeholder"></span> <?php echo $_product->getNameCustom(); ?></h1>
我假设其目的是使所选选项文本显示在
中。如果是这样,那么为什么不在.on()
回调中执行以下操作:
var disMetalType = console.log(this.options[e.target.selectedIndex].text);
$('#custom_product_name span').text(disMetalType);
这需要将您的
更改为:
<h1 id="custom_product_name">
<span></span>
<?php echo $_product->getNameCustom(); ?>
</h1>
我假设其目的是使所选选项文本显示在
中。如果是这样,那么为什么不在.on()
回调中执行以下操作:
var disMetalType = console.log(this.options[e.target.selectedIndex].text);
$('#custom_product_name span').text(disMetalType);
这需要将您的
更改为:
<h1 id="custom_product_name">
<span></span>
<?php echo $_product->getNameCustom(); ?>
</h1>
您想根据下拉菜单的选择更改标题文本。请尝试以下方法:(这只是一个示例,请根据您的要求进行更改) HTML:
您想根据下拉菜单的选择更改标题文本。请尝试以下方法:(这只是一个示例,请根据您的要求进行更改) HTML:
我尝试了第二个选项,但仍然不起作用。您在Chrome或Firefox(F12)中是否遇到任何控制台错误?console.log输出的数字是否正确?我尝试了第二个选项,但仍然不起作用。您在Chrome或Firefox(F12)中是否遇到任何控制台错误?console.log是否输出了正确的数字?您最好检查控制台中弹出的错误。您最好检查控制台中弹出的错误。