简体   繁体   English

如何使用jQuery更改跨度文本

[英]How to change text in span using Jquery

I'm pretty new to Ajax and JQuery. 我对Ajax和JQuery很陌生。 I'm trying to change the value of a span when I type text in an input box but nothing is working(ie the value in the span is not changing). 当我在输入框中键入文本但没有任何作用时(例如,范围中的值未更改),我试图更改范围的值。 After some research around StackOverflow, I found out that I should either use the text() or html() of JQuery to change the value but it still does not change. 在对StackOverflow进行了一些研究之后,我发现我应该使用JQuery的text()html()来更改值,但仍然不会更改。 I'm not sure where I went wrong. 我不确定哪里出了问题。 Below is my code: 下面是我的代码:

Html of Input Box: 输入框的HTML:

<input class="amnt" id ="Red:XL:50:24.55" type="text">
<span id="50-Red-24.55">0(This text is not changing)</span>

Here's my JQuery. 这是我的JQuery。 It first performs an Ajax(I omitted this part) and then after Ajax success, it should change the value of my span: 它首先执行一个Ajax(我省略了这一部分),然后在Ajax成功之后,它应该更改我的span的值:

$( ".amnt" ).keyup(function() {
    var split = this.id.split(":");
    var color = split[0];
    var size = split[1];
    var ID = split[2];
    var price = split[3];

    $.ajax({    //create an ajax request to getData.php
        success: function(response){                  

            $("#"+ID+"-"+color+"-"+price).text(price);
        },
        error:function (xhr, ajaxOptions, thrownError){
         alert(thrownError);
        }
    });
});

I also tried $("#"+ID+"-"+color+"-"+price).html(price); 我还尝试了$("#"+ID+"-"+color+"-"+price).html(price); instead of $("#"+ID+"-"+color+"-"+price).text(price); 而不是$("#"+ID+"-"+color+"-"+price).text(price); but nothig changes on keyup . 但是nothig更改了keyup Can anyone please explain what I'm doing wrong here? 任何人都可以在这里解释我做错了什么吗?

Thanks. 谢谢。

Instead of concatenating different items to create the Id string, It would be better and clean, if you use relative selectors in your code. 如果您在代码中使用相对选择器,那么最好不要合并其他项来创建Id字符串。

Add a css class to your message span and wrap this in a container div. 将CSS类添加到您的消息范围中,并将其包装在容器div中。 Also instead of keeping your data inside the Id value, you may keep those in HTML 5 data attributes. 同样,也可以将数据保留在HTML 5数据属性中,而不是将数据保留在Id值之内。

<div class="amtContainer">
  <input class="amnt" data-size="XL" data-price="24.55" type="text">
  <span class="amtMsg">0(This text is not changing)</span>
</div>

And in your script, use the closest() method to get a reference to the outer container div, and then use find() method to get to the message span. 在您的脚本中,使用closest()方法获取对外部容器div的引用,然后使用find()方法获取消息跨度。

$( ".amnt" ).keyup(function() {
    var _this= $(this);
    var val=_this.val();

    var price=_this.data("price");

    $.ajax({    //create an ajax request to getData.php
        success: function(response){                  

           _this.closest(".amtContainer").find(".amtMsg").text(price);
        },
        error:function (xhr, ajaxOptions, thrownError){
         alert(thrownError);
        }
    });
});

First create the id concatenating the variables. 首先创建连接变量的id Then replace . 然后更换. with \\\\. \\\\. and use the modified id to select the span element. 并使用修改后的ID选择span元素。 Then use .html() to change text in the span. 然后使用.html()更改跨度中的文本。

You can use this function to escape css selector characters from your id string 您可以使用此功能从ID字符串中转义CSS选择器字符

function jq( myid ) {

    return myid.replace( /(:|\.|\[|\]|,)/g, "\\$1" );

}

Reference: https://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/ 参考: https : //learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-符号/

The problem (credits to @adeneo): 问题(贷记@adeneo):

In the interpolated selector string, jQuery sees periods (which are perfectly fine ID characters) as class names, so something like "#my.cool.id" would actually select this element: 在插补的选择器字符串中,jQuery将句点(完全是很好的ID字符)视为类名,因此类似"#my.cool.id"类的东西实际上会选择此元素:

<div id="my" class="cool id"></div>

and not this one: 不是这个:

<div id="my.cool.id"></div>

The solution: 解决方案:

To avoid that issue, simply escape or replace the period in your price part of the ID with any legal ID character that's not a number or a period or a colon (since you're usign it for splitting), for example, 24X55 , 24|55 , etc. will all work fine. 为了避免这个问题,简单地逃避或更换周期与任何合法ID字符,这不是一个数字或一个句点或冒号(因为你usign它分裂)的ID你的价格部分,例如, 24X5524|55等都可以正常工作。

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

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