Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于更改jquery函数的问题_Jquery_Wordpress_Onchange - Fatal编程技术网

关于更改jquery函数的问题

关于更改jquery函数的问题,jquery,wordpress,onchange,Jquery,Wordpress,Onchange,我面临着非常奇怪的问题。我有一个下拉选项 <table class="variations"> <tr> <td> <select name="up_options"> <option value="" selected>Select Value</option> <option value="a">A</option

我面临着非常奇怪的问题。我有一个下拉选项

<table class="variations">
   <tr>
      <td>
          <select name="up_options">
             <option value="" selected>Select Value</option>
             <option value="a">A</option>
             <option value="b">B</option>
             <option value="c">C</option>
             <option value="d">D</option>
             <option value="e">E</option>
          </select>
      </td>
   </tr>
</table>
但它也不起作用

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('.variations select').change(function(){
           alert(jQuery('.single_variation').text());
        });
    });
</script>
请帮帮我,我对此很困惑

试试这个

 jQuery('select[name="up_options"]').change(function(){
      jQuery('.single_variation').text( $(this).val());
      alert(jQuery('.single_variation').text());
  });

jQuery在当前元素方面非常好,比如
$(this)
或者
jQuery(this)
,所以使用like

alert(jQuery(this).text()); // to alert the content of the selected item

alert(jQuery(this).val()); // to alert the value of the selected item

事件被触发,结果将根据
选择
选项显示。

您可以这样尝试 使用委托绑定事件是合适的最佳做法。

原始代码没有“错误”,但您可以进行以下改进:

   // Shortcut for DOM ready with locally scoped $
   jQuery(function($) {

        // Delegated event handler attached to a common ancestor
        $('.variations').on('change','select',function(){

            // this is the select itself, so use its val()
           var currentSelectVal = $(this).val();

           // Do something with the selection
           $('.single_variation').text(currentSelectVal);
        });
    });
JSFiddle:


注意:我期待看到页面的其余部分和代码,以确定您的实际问题所在:)

这里没有不应该出现的问题。您能否在中提供一个问题的工作示例。什么是
.single_variation
?在这里找到您的答案,为select本身添加一个标识符,并将onchange绑定到该选项。无法解释为什么though@HuzoorBuxOP希望获得不同元素的值,而不是select,而且它们所拥有的值应该可以正常工作。这应该没有什么区别(除了稍微降低选择器的速度)。原始代码应该可以工作。另外请注意:您中途切换到使用
$
。您必须与jQuery的OPs用法保持一致。问题是更改事件未触发,而不是警报。请在您的应用程序中添加任何相关的更改代码answer@TrueBlueAussie我检查了它,它将返回整个文本对不起,我的意思是:
jQuery(this.val()有什么问题
:)不建议将委派事件绑定到
正文
,因为它有一个与样式相关的错误(如果由于样式设置,计算的高度为0,则不会获取某些事件)。如果没有比这更接近的东西,则使用
document
作为默认值(在您的示例中使用
jQuery('.variances')。on('change','select',function(){
更有意义,效率更高。其次,原始代码也可以工作,因此问题存在于尚未发现的地方:)thanx很多兄弟你救了我的命和我的一天…我对你的代码和它的工作做了一些完美的修改
jQuery(document).ready(function() {
        jQuery('.variations select').change(function(){
        $(".single_variation").text((jQuery('.variations select option:selected').text()));    
        });
    });
$(".single_variation").text($(this).val().toUpperCase());
 jQuery(document).ready(function() {
        jQuery('body').on('change','.variations select[name="up_options"]',function(){
           alert(jQuery('.single_variation').text());
           alert(jQuery
(this).val()); //Current Drowndown value
        });
    });
   // Shortcut for DOM ready with locally scoped $
   jQuery(function($) {

        // Delegated event handler attached to a common ancestor
        $('.variations').on('change','select',function(){

            // this is the select itself, so use its val()
           var currentSelectVal = $(this).val();

           // Do something with the selection
           $('.single_variation').text(currentSelectVal);
        });
    });