简体   繁体   English

JavaScript在跨度中更改值和值中的数据

[英]JavaScript change value and data from value in a span

I need help with this. 我需要这方面的帮助。 I need to make the value in the other place change all the time while user session is active. 在用户会话处于活动状态时,我需要始终在其他位置更改值。 How can I get the value from a span and make other value in a data change? 如何从范围中获取值并在数据更改中获取其他值?

Look at there! 看那里!

 1 <div class="pt-uea-container">
 2  <span class="pt-uea-currency pt-uea-currency-before"> € </span>
 3  <input type="text" class="pt-field pt-uea-custom-amount" autocomplete="off" name="pt_items[1][amount]" id="pt_uea_custom_amount_1" value="199" placeholder="" data-parsley-errors-container="#pt_uea_custom_amount_errors_1">
 4  <input type="hidden" class="pt-field pt-uea-custom-amount-formatted" name="pt_items[1][amount]" value="199" data-pt-price="199">
 5  <input type="hidden" name="pt_items[1][label]" value="Amount:">
 6  <input type="hidden" name="pt_items[1][tax_percentage]" value="0">
 7  <input type="hidden" name="pt_items[1][type]" value="open">
 8  <div id="pt_uea_custom_amount_errors_1"></div>
 9  <span class="form-price-value">85</span>
10 </div>

The value in row 9 needs to constantly change values in row 3 and 4 on the same session. 第9行中的值需要在同一会话中不断更改第3行和第4行中的值。 Don't mind the value in row 6. 不要介意第6行中的值。

Let me know how I can get this done. 让我知道如何完成这项工作。 Or maybe a different approach? 还是不同的方法?

Greetings! 问候!

======== ========

So this is what I got for now from you guys: 所以这是我现在从你们这里得到的:

jQuery(document).ready(function($) {
var checkViewport = setInterval(function() {
var spanVal = $('.form-price-value').text();
$('#pt_uea_custom_amount_1').val(spanVal);
$('#pt_uea_custom_amount_formatted_1').val(spanVal);
$('#pt_uea_custom_amount_formatted_1').attr('data-pt-price', spanVal);
}, 1000);

});

This code works, but it only affects my needs when I put my mouse in pt-field pt-uea-custom-amount and add a space in it. 这段代码有效,但是仅当我将鼠标放在pt字段pt-uea-custom-amount中并在其中添加空格时,它才会影响我的需求。 Then it does apply to the page source. 然后,它确实适用于页面源。 But this is not correct. 但这是不正确的。 The source needs to get changed too without touching that class or a space or something! 源代码也需要更改,而不必碰到该类,空格或其他东西!

This is not an ideal solution. 这不是理想的解决方案。 I'm not sure there is a verified way of listening for when the innerHTML of a span element changes. 我不确定span元素的innerHTML更改时是否有经过验证的侦听方式。 This sort of stuff is usually based on user interaction, and the value of the span will be modified by your page. 这类内容通常基于用户交互,并且跨度的值将由您的页面修改。 The best solution would be to use the same method that updates the span element to update the values of you hidden input fields. 最好的解决方案是使用与更新span元素相同的方法来更新隐藏输入字段的值。 However, I've placed an interval that will run every second, that takes the text value of the span element and gives it to the values of the 2 input fields: 但是,我放置了一个间隔,该间隔将每秒运行一次,该间隔采用span元素的文本值并将其赋予2个输入字段的值:

 function start() { setInterval(function() { document.getElementById("pt_uea_custom_amount_1").value = document.getElementById("price_value").innerHTML; document.getElementById("pt_uea_custom_amount_2").value = document.getElementById("price_value").innerHTML; }, 1000); } window.onload = start(); 
 <div class="pt-uea-container"> <span class="pt-uea-currency pt-uea-currency-before"> € </span> <input type="text" class="pt-field pt-uea-custom-amount" autocomplete="off" name="pt_items[1][amount]" id="pt_uea_custom_amount_1" value="199" placeholder="" data-parsley-errors-container="#pt_uea_custom_amount_errors_1"> <input type="hidden" class="pt-field pt-uea-custom-amount-formatted" name="pt_items[1][amount]" value="199" data-pt-price="199" id="pt_uea_custom_amount_2"> <input type="hidden" name="pt_items[1][label]" value="Amount:"> <input type="hidden" name="pt_items[1][tax_percentage]" value="0"> <input type="hidden" name="pt_items[1][type]" value="open"> <div id="pt_uea_custom_amount_errors_1"></div> <span id="price_value" class="form-price-value">85</span> </div> 

try this, simple using jquery, you can check in inspect element for value attribute data-pt-price 试试这个,简单的使用jQuery,您可以检入检查元素的值属性data-pt-price

Update: you can using jquery event .on() like change, click, keyup or else to Attach an event handler function for one or more events to the selected elements, you can read the doc here . 更新:您可以使用jquery event .on()更改,单击,键入或其他操作,以将一个或多个事件的事件处理函数附加到所选元素上,您可以在此处阅读文档。

here the updated code 这是更新的代码

 $(function() { var spanVal = $('#price_value').text(); $('#pt_uea_custom_amount_1').val(spanVal); $('#pt_uea_custom_amount_formatted_1').val(spanVal); $('#pt_uea_custom_amount_formatted_1').attr('data-pt-price', spanVal); $('#pt_uea_custom_amount_1').on('change click keyup', function() { $('#pt_uea_custom_amount_formatted_1').val($(this).val()); $('#price_value').text($(this).val()); $('#pt_uea_custom_amount_formatted_1').attr('data-pt-price', $(this).val()); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="pt-uea-container"> <span class="pt-uea-currency pt-uea-currency-before"> € </span> <input type="text" class="pt-field pt-uea-custom-amount" autocomplete="off" name="pt_items[1][amount]" id="pt_uea_custom_amount_1" value="199" placeholder="" data-parsley-errors-container="#pt_uea_custom_amount_errors_1"> <input type="hidden" class="pt-field pt-uea-custom-amount-formatted" name="pt_items[1][amount]" value="199" data-pt-price="199" id="pt_uea_custom_amount_2"> <input type="hidden" name="pt_items[1][label]" value="Amount:"> <input type="hidden" name="pt_items[1][tax_percentage]" value="0"> <input type="hidden" name="pt_items[1][type]" value="open"> <div id="pt_uea_custom_amount_errors_1"></div> <span id="price_value" class="form-price-value">85</span> </div> 

You can easily do this with the help of jQuery . 您可以借助jQuery轻松地做到这一点。

With the help of jQuery I would do like this. 在jQuery的帮助下,我会这样做。

  1. Understanding what input field needs to be tracked for changes. 了解需要跟踪哪些输入字段以进行更改。 I will give all this field a class ( track-me ). 我将在所有这些领域上一堂课( track-me )。
  2. In the document ready, I will look for changes for that tracked field. 在准备好的文档中,我将寻找该跟踪字段的更改。
  3. On change of that field I will get the value and put in other input fields (class copy-to - or you can do whatever you like). 更改该字段后,我将获得该值并将其输入其他输入字段(类copy-to -或您可以执行任何操作)。

See an example below, 参见下面的示例,

HTML 的HTML

<form>
    <div class="">
      <input type="text" class="track-me" value=""/>
    </div>
    <div class="">
      <input type="text" class="copy-to" value=""/>
    </div>
    <div class="">
       <input type="text" class="copy-to" value=""/>
    </div>
    <div class="">
      <input type="text" class="copy-to" value=""/>
    </div> 
    <div class="">
      <div class="">Please type anything in the first input box</div>
    </div>
  </form>

jQuery jQuery的

$(document).ready(function(){
    $('.track-me').change(function (){
        $('.copy-to').val($(this).val())
    });
});

I made comments in the above jQuery code so you can understand. 我在上面的jQuery代码中添加了注释,以便您理解。 Also, I have made a fiddle so you can play and have a look. 另外,我做了一个小提琴,让您可以玩耍,看看。 In this fiddle, I am using Bootstrap4 just for the purpose of styling, you don't have to worry about that. 在这个小提琴中,我只是出于样式目的而使用Bootstrap4 ,您不必为此担心。

Link to fiddle 链接到小提琴

https://jsfiddle.net/anjanasilva/r21u4fmh/21/ https://jsfiddle.net/anjanasilva/r21u4fmh/21/

I hope this helps. 我希望这有帮助。 Feel free to ask me any questions if you have. 如有问题,请随时问我。 Cheers. 干杯。

MutationObserver should work here.. MutationObserver应该在这里工作。

const formValuePrice = document.querySelector( '.form-price-value' );
const inputText = document.querySelector( 'input[type="text"]' );

// timer to change values
window.setInterval( () => {
    formValuePrice.textContent = Math.round( Math.random() * 100 );
}, 1000 );

// mutation observer
const observer = new MutationObserver( ( mutationsList ) => {
    inputText.value = formValuePrice.textContent;
} );

observer.observe( formValuePrice, { childList: true } );

https://codepen.io/anon/pen/LgWXrz?editors=1111 https://codepen.io/anon/pen/LgWXrz?editors=1111

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

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