在jQuery中替换变量仍然使用先前的值。

huangapple go评论87阅读模式
英文:

Replacing variables in jQuery keeps using the prior value

问题

以下是您要翻译的内容:

我有下一个输入,默认情况下显示“购买”值

<input class="myVar" data-buy="50000" data-sell="80000" value="5000" />
<button id="change">更改价格</button>
<button id="toggle">切换</button>

我点击“#change”以增加“购买”和“销售”的金额

$(document).on("click", "#change", function(){
    $(".myVar").attr("data-buy", 6000);
    $(".myVar").attr("data-sell", 9000);
});

现在,如果我切换到“myVar”输入中显示的值将恢复到默认值。

$("#toggle").toggle(
    function(){
        $(".myVar").val($(".myVar").data("buy"));
    },
    function(){ 
        $(".myVar").val($(".myVar").data("sell"));
    }
);

我做错了什么?

我知道变量需要设置在DOM上,

我尝试了

$(document).on("click", ".change", function(){
    $(".myVar").attr("data-buy", 6000);
    $(".myVar").attr("data-sell", 9000);
    $("#toggle").click();
});

但它改变了行为,但仍然显示初始值。

请注意,代码部分已被忽略,只提供翻译的内容。

英文:

I have the next input that shows "buy" value by default

    &lt;input class=&quot;myVar&quot; data-buy=&quot;50000&quot; data-sell=&quot;80000&quot; value=&quot;5000&quot; /&gt;
    &lt;button id=&quot;change&quot;&gt;CHANGE PRICES&lt;/button&gt;
    &lt;button id=&quot;toggle&quot;&gt;TOGGLE&lt;/button&gt;

I click on "#change" in order to increase the amount of "buy" and "sell"

     $(document).on(&quot;click&quot;, #change&quot;, function(){
         $(&quot;.myVar&quot;).attr(&quot;data-buy&quot;,6000);
         $(&quot;.myVar&quot;).attr(&quot;data-sell&quot;,9000);
      
     });

Now if I toggle the value shown in "myVar" input restores to the default values.

      $(&quot;#toggle&quot;).toggle(
     function(){
   
       $(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).data(&quot;buy&quot;));
       },
      function(){ 
      $(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).data(&quot;sell&quot;));
      }

      );

What am I doing wrong?

I know the variables need to be set on the DOM,

I tried

       $(document).on(&quot;click&quot;, &quot;.change&quot;, function(){
         $(&quot;.myVar&quot;).attr(&quot;data-buy&quot;,6000);
         $(&quot;.myVar&quot;).attr(&quot;data-sell&quot;,9000);
                    $(&quot;#toggle&quot;).click()
      
     }); 

but it changes the behaviour but it still show the initial values.

答案1

得分: 1

移除 $(...).data 并使用 $(...).attr(&quot;data-type&quot;,...)

这是我所做的。

$(document).ready(function(){
  // CHANGE PRICES click
  $("#change").click(function(){
    $(".myVar").attr("data-buy", 6000);
    $(".myVar").attr("data-sell", 9000);
  });
  
  // toggle click
  $("#toggle").click(function(){
    if($(this).attr("data-type") === 'buy'){
      $(".myVar").val($(".myVar").attr("data-buy"));
      $(this).attr("data-type", 'sell');
    } else {
      $(".myVar").val($(".myVar").attr("data-sell"));
      $(this).attr("data-type", 'buy');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="myVar" data-buy="50000" data-sell="80000" value="50000" />
<button id="change">CHANGE PRICES</button>
<button data-type="sell" id="toggle">TOGGLE</button>

请注意,代码部分没有翻译。

英文:

Remove $(...).data and use $(...).attr(&quot;data-type&quot;,...)

This is what I did.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function(){
  // CHANGE PRICES click
  $(&quot;#change&quot;).click(function(){
    $(&quot;.myVar&quot;).attr(&quot;data-buy&quot;,6000);
         $(&quot;.myVar&quot;).attr(&quot;data-sell&quot;,9000);
  });
  
  // toggle click
  $(&quot;#toggle&quot;).click(function(){
    if($(this).attr(&quot;data-type&quot;) === &#39;buy&#39;){
    	$(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).attr(&quot;data-buy&quot;));
        $(this).attr(&quot;data-type&quot;,&#39;sell&#39;);
    }else{
    	$(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).attr(&quot;data-sell&quot;));
        $(this).attr(&quot;data-type&quot;,&#39;buy&#39;);
    }
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input class=&quot;myVar&quot; data-buy=&quot;50000&quot; data-sell=&quot;80000&quot; value=&quot;50000&quot; /&gt;
    &lt;button id=&quot;change&quot;&gt;CHANGE PRICES&lt;/button&gt;
    &lt;button data-type=&quot;sell&quot; id=&quot;toggle&quot;&gt;TOGGLE&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 1

你的代码中有一些问题。

第一个问题实际上是你的环境中唯一的问题,我认为。 使用 .data() 是访问和操作数据属性的正确方式,但我注意到你的代码混合了使用 .data().attr() 来访问相同的值,例如:

$(".myVar").attr("data-buy", 6000);

$(".myVar").val($(".myVar").data("buy"));

只需将使用 .attr() 的部分替换为一致使用 .data() 即可使你的代码按照你的描述工作(对于旧版本的 jQuery,请参见下一条)。以下是使用 jQuery 1.8 的工作示例。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
$(document).on("click", "#change", function() {
  $(".myVar").data("buy", 6000);
  $(".myVar").data("sell", 9000);
});

$("#toggle").toggle(
  function() {
    $(".myVar").val($(".myVar").data("buy"));
  },
  function() {
    $(".myVar").val($(".myVar").data("sell"));
  }
);

<!-- 语言:lang-html -->
<script src="https://code.jquery.com/jquery-1.8.0.min.js"></script>
<input class="myVar" data-buy="50000" data-sell="80000" value="5000" />
<button id="change">CHANGE PRICES</button>
<button id="toggle">TOGGLE</button>

<!-- 结束代码片段 -->

第二个问题与 .toggle() 相关。在旧版本的 jQuery 中,有一个 toggle() 事件处理程序,但它很早就被弃用了(从2012年版本1.8开始!)。你可能正在使用一个古老的 jQuery 版本,其中仍然可以使用这个功能,但出于安全原因,你应该考虑升级。

在任何不到10年的 jQuery 版本中,.toggle() 将显示或隐藏一个元素。在你的代码中,这意味着 <kbd>toggle</kbd> 按钮在页面加载时立即消失。以下是上面代码的副本,仅更改为使用当前版本的 jQuery,演示了这一点。

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
$(document).on("click", "#change", function() {
  $(".myVar").data("buy", 6000);
  $(".myVar").data("sell", 9000);
});

$("#toggle").toggle(
  function() {
    $(".myVar").val($(".myVar").data("buy"));
  },
  function() {
    $(".myVar").val($(".myVar").data("sell"));
  }
);

<!-- 语言:lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input class="myVar" data-buy="50000" data-sell="80000" value="5000" />
<button id="change">CHANGE PRICES</button>
<button id="toggle">TOGGLE</button>

<!-- 结束代码片段 -->

如果以后升级 jQuery,你将需要更新这段代码。据我所知,你实际上只需要一个点击处理程序来触发显示其中一个值或另一个值。关键是你需要跟踪当前显示的是哪个值,以便在点击按钮时显示另一个值。也许我们可以在输入框上使用另一个 data 属性来跟踪当前显示的值,类似于以下内容:

<input class="myVar" data-showing="" data-buy="50000" data-sell="80000" value="5000" />

然后,当点击 <kbd>toggle</kbd> 按钮时,你可以检查当前显示的是哪个值,加载并显示另一个值,然后更新该属性以保存当前状态。请注意,我们需要一个额外的情况来处理初始状态,当显示的值既不是购买值也不是销售值时(我不确定这是一个拼写错误还是实际需要的情况)。

以下是一个示例:

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-js -->
$(document).on("click", "#change", function () {
    $(".myVar").data("buy", 6000);
    $(".myVar").data("sell", 9000);
});

$("#toggle").on('click', function () {
    let val = '';
    let showing = $(this).data('showing');
    
    // 处理购买情况和初始情况,当显示的值既不是销售值也不是购买值时
    if (showing === 'buy' || showing === '') {
        val = $(".myVar").data("sell");
        showing = 'sell';
    } else {
        val = $(".myVar").data("buy");
        showing = 'buy';
    }
    
    $(".myVar").val(val);
    $(this).data('showing', showing);
});

<!-- 语言:lang-html -->
<script src="https://code.jquery.com/jquery-1.8.0.min.js"></script>
<input class="myVar" data-showing="" data-buy="50000" data-sell="80000" value="5000" />
<button id="change">CHANGE PRICES</button>
<button id="toggle">TOGGLE</button>

<!-- 结束代码片段 -->
英文:

There are a few problems in your code.

The first is really the only problem for you in your environment, I think. Using .data() is the correct way to access and manipulate data attributes, but I noticed that your code mixes using .data() and .attr() to access the same values, eg:

$(&quot;.myVar&quot;).attr(&quot;data-buy&quot;,6000);

and

$(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).data(&quot;buy&quot;));

Replacing the cases where you're using .attr() to instead consistently use .data() is all that is needed to get your code working as you describe (with old versions of jQuery, see next item). Here's a working snippet using jQuery 1.8.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).on(&quot;click&quot;, &quot;#change&quot;, function() {
  $(&quot;.myVar&quot;).data(&quot;buy&quot;, 6000);
  $(&quot;.myVar&quot;).data(&quot;sell&quot;, 9000);
});

$(&quot;#toggle&quot;).toggle(
  function() {
    $(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).data(&quot;buy&quot;));
  },
  function() {
    $(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).data(&quot;sell&quot;));
  }
);

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-1.8.0.min.js&quot;&gt;&lt;/script&gt;
&lt;input class=&quot;myVar&quot; data-buy=&quot;50000&quot; data-sell=&quot;80000&quot; value=&quot;5000&quot; /&gt;
&lt;button id=&quot;change&quot;&gt;CHANGE PRICES&lt;/button&gt;
&lt;button id=&quot;toggle&quot;&gt;TOGGLE&lt;/button&gt;

<!-- end snippet -->

The second problem relates to .toggle(). In old versions of jQuery, there was a toggle() event handler, but it was deprecated long ago (in version 1.8, from 2012!). Presumably you are using an ancient version of jQuery where this still works, but for security reasons alone you should probably upgrade.

In any version of jQuery less than 10 years old, .toggle() will show or hide an element. In your code that means the <kbd>toggle</kbd> button just disappears immediately on page load. Here's a copy of the above snippet, changed only to use the current version of jQuery, which demonstrates that.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).on(&quot;click&quot;, &quot;#change&quot;, function() {
  $(&quot;.myVar&quot;).data(&quot;buy&quot;, 6000);
  $(&quot;.myVar&quot;).data(&quot;sell&quot;, 9000);
});

$(&quot;#toggle&quot;).toggle(
  function() {
    $(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).data(&quot;buy&quot;));
  },
  function() {
    $(&quot;.myVar&quot;).val($(&quot;.myVar&quot;).data(&quot;sell&quot;));
  }
);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;input class=&quot;myVar&quot; data-buy=&quot;50000&quot; data-sell=&quot;80000&quot; value=&quot;5000&quot; /&gt;
&lt;button id=&quot;change&quot;&gt;CHANGE PRICES&lt;/button&gt;
&lt;button id=&quot;toggle&quot;&gt;TOGGLE&lt;/button&gt;

<!-- end snippet -->

If and when you do upgrade jQuery, you'll need to update this code too. All you really need is a click handler, AFAICT, to trigger display of one value or the other. The catch is you need to track which one is currently displayed, so that you can show the other one when the button is clicked. Maybe we can use another data attribute on the input to track which is currently being displayed, something like:

&lt;input class=&quot;myVar&quot; data-showing=&quot;buy&quot; ...

Then when you click the <kbd>toggle</kbd> button you can check which is currently shown, load and show the other, and then update that attribute to save the current state. Note we need an extra case to handle the initial state, when the displayed value is neither the buy value nor the sell value (I'm not sure if that is just a typo or actually required).

Here's an example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).on(&quot;click&quot;, &quot;#change&quot;, function () {
    $(&quot;.myVar&quot;).data(&quot;buy&quot;, 6000);
    $(&quot;.myVar&quot;).data(&quot;sell&quot;, 9000);
});

$(&quot;#toggle&quot;).on(&#39;click&#39;, function () {
    let val = &#39;&#39;;
    let showing = $(this).data(&#39;showing&#39;);
    
    // Handle both buy case and initial case when displayed value is neither sell or buy value
    if (showing === &#39;buy&#39; || showing === &#39;&#39;) {
        val = $(&quot;.myVar&quot;).data(&quot;sell&quot;);
        showing = &#39;sell&#39;;
    } else {
        val = $(&quot;.myVar&quot;).data(&quot;buy&quot;);
        showing = &#39;buy&#39;;
    }
    
    $(&quot;.myVar&quot;).val(val);
    $(this).data(&#39;showing&#39;, showing);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-1.8.0.min.js&quot;&gt;&lt;/script&gt;
&lt;input class=&quot;myVar&quot; data-showing=&quot;&quot; data-buy=&quot;50000&quot; data-sell=&quot;80000&quot; value=&quot;5000&quot; /&gt;
&lt;button id=&quot;change&quot;&gt;CHANGE PRICES&lt;/button&gt;
&lt;button id=&quot;toggle&quot;&gt;TOGGLE&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 09:57:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定