jQuery:如何处理span的更改文本

时间:2022-11-13 18:41:09

I'm using jQuery and I want to show some calcution in a span (called span1) and I want when text of span1 changed do some calcution on it's value and show in other spans(called `span2 ,span3,...). How I can handle text change of span?

我正在使用jQuery,我想在一个范围内显示一些计算(称为span1),我想在span1的文本更改时对其值进行一些计算并在其他跨度中显示(称为“span2,span3,...”)。我如何处理跨度的文本更改?

thanks

谢谢

2 个解决方案

#1


15  

You could use the function that changes the text of span1 to change the text of the others.

您可以使用更改span1文本的函数来更改其他文本。

As a work around, if you really want it to have a change event, then don't asign text to span 1. Instead asign an input variable in jQuery, write a change event to it, and whever ur changing the text of span1 .. instead change the value of your input variable, thus firing change event, like so:

作为一种解决方法,如果你真的希望它有一个更改事件,那么不要将文本设置为跨度1.而是在jQuery中对输入变量进行设置,将更改事件写入其中,并更改为span1的文本。而是更改输入变量的值,从而触发更改事件,如下所示:

var spanChange = $("<input />");
function someFuncToCalculateAndSetTextForSpan1() {
    //  do work
    spanChange.val($newText).change();
};

$(function() {
    spanChange.change(function(e) {
        var $val = $(this).val(),
            $newVal = some*calc-$val;
        $("#span1").text($val);
        $("#spanWhatever").text($newVal);
    });
});

Though I really feel this "work-around", while useful in some aspects of creating a simple change event, is very overextended, and you'd best be making the changes to other spans at the same time you change span1.

虽然我真的觉得这种“解决方法”虽然在创建简单更改事件的某些方面很有用,但是非常过度扩展,并且您最好在更改span1的同时对其他跨度进行更改。

#2


25  

Span does not have 'change' event by default. But you can add this event manually.

默认情况下,Span没有“更改”事件。但您可以手动添加此事件。

Listen to the change event of span.

聆听跨度的变化事件。

$("#span1").on('change',function(){
     //Do calculation and change value of other span2,span3 here
     $("#span2").text('calculated value');
});

And wherever you change the text in span1. Trigger the change event manually.

无论你在哪里更改span1中的文本。手动触发更改事件。

$("#span1").text('test').trigger('change'); 

#1


15  

You could use the function that changes the text of span1 to change the text of the others.

您可以使用更改span1文本的函数来更改其他文本。

As a work around, if you really want it to have a change event, then don't asign text to span 1. Instead asign an input variable in jQuery, write a change event to it, and whever ur changing the text of span1 .. instead change the value of your input variable, thus firing change event, like so:

作为一种解决方法,如果你真的希望它有一个更改事件,那么不要将文本设置为跨度1.而是在jQuery中对输入变量进行设置,将更改事件写入其中,并更改为span1的文本。而是更改输入变量的值,从而触发更改事件,如下所示:

var spanChange = $("<input />");
function someFuncToCalculateAndSetTextForSpan1() {
    //  do work
    spanChange.val($newText).change();
};

$(function() {
    spanChange.change(function(e) {
        var $val = $(this).val(),
            $newVal = some*calc-$val;
        $("#span1").text($val);
        $("#spanWhatever").text($newVal);
    });
});

Though I really feel this "work-around", while useful in some aspects of creating a simple change event, is very overextended, and you'd best be making the changes to other spans at the same time you change span1.

虽然我真的觉得这种“解决方法”虽然在创建简单更改事件的某些方面很有用,但是非常过度扩展,并且您最好在更改span1的同时对其他跨度进行更改。

#2


25  

Span does not have 'change' event by default. But you can add this event manually.

默认情况下,Span没有“更改”事件。但您可以手动添加此事件。

Listen to the change event of span.

聆听跨度的变化事件。

$("#span1").on('change',function(){
     //Do calculation and change value of other span2,span3 here
     $("#span2").text('calculated value');
});

And wherever you change the text in span1. Trigger the change event manually.

无论你在哪里更改span1中的文本。手动触发更改事件。

$("#span1").text('test').trigger('change');