简体   繁体   English

如何在不更改其当前元素的情况下更改其文本?

[英]How can I change an element's text without changing its current element?

在此处输入图片说明

$.ajax({
    type: "GET",
    url: "my url" + x,
    datatype: "html",
    success: function(data){
        //alert(x);
         //var Content = data;
         var sendId ;
         var data = $.parseJSON(data);
         var jsonArray = data.result;

        // var data2 = $.parseJSON(jsonArray);
         var jsonArray3 = jsonArray.oilFilter;
         $.each(jsonArray3,function(i1,js2){
             var proname = js2.productName;
             var mrp = js2.mrp;
             var deliveryStatus = js2.deliveryStatus;
             var oilid = js2.productId;
             $(".oilid").val(oilid);
             $(".deliveryStatusOil").html(deliveryStatus);
             $(".mrpoil").html(mrp);
             $(".oilprodetail").html(proname);


         });

    <label id="oilprodetail" class="oilprodetail"></label><br/>
    <label id="mrpoil" class="mrpoil"></label><br/>
    <a onClick="ajax_cross(this);" id="oilid" class="oilid" >View</a>

If you go through the code I am assigning values to label tag with id oilprodetail and mrpoil and i am doing the same thing to oilid as well, My requirement is to send oilid value to a tag and retain view there, Please help? 如果您遍历代码,我将给id标签为oilprodetail和mrpoil的标签分配值,并且我也对oilid做同样的事情,我的要求是将oilid值发送到标签并在其中保留视图,请帮忙吗? I am new to ajax.. 我是ajax的新手。

在此处输入图片说明

In the above picture i need to do the right part of the pic that is view,, but when i send the data it changes to left part of the pic like it changes from view to the value that is sent 在上面的图片中,我需要在图片的右侧进行查看,但是当我发送数据时,它会更改为图片的左侧,就像它从视图更改为发送的值一样

You can use data attribute to keep innerHTML of "a" tag. 您可以使用data属性将innerHTML保留为“ a”标签。 Like : 喜欢 :

$(".oilid").attr('data-oilid',oilid);

There are several issues with your code: 您的代码有几个问题:

  • You misspelled datatype , which should be dataType ; 您拼写错误的datatype ,应该是dataType
  • Even if correctly spelled, you then wrongly identify the data type as HTML: you really expect JSON, which is something different; 即使拼写正确,您也会错误地将数据类型标识为HTML:您确实期望JSON,这是不同的;
  • Without the dataType property (which is your case), jQuery will guess the data type, and probably will guess it to be JSON. 没有dataType属性(这是您的情况),jQuery将猜测数据类型,并且可能会猜测它是JSON。 In that case, $.parseJSON will fail, because jQuery will already have decoded the JSON string and present the result as the data argument; 在这种情况下, $.parseJSON将失败,因为jQuery将已经解码了JSON字符串并将结果显示为data参数。
  • You use the .html(...) method to set the content of some elements to some text. 您可以使用.html(...)方法将某些元素的内容设置为某些文本。 html() should only be used when the content really is HTML, but otherwise (which I think is your case), you should .text(...) ; html()仅应在内容确实为HTML时使用,否则(我认为是您的情况)应使用.text(...) ;
  • An a element does not have a value property, so calling .val() on it does not have the desired effect; a元素没有value属性,因此在其上调用.val()并没有达到预期的效果。
  • You want to pass the oilid value to the ajax_cross function, but provide it this , which is an HTML element (the a ), not the value. 您想将oilid值传递给ajax_cross函数,但要向其提供this ,它是HTML元素( a ),而不是值。
  • As you iterate the array, you are setting the content of the exact same elements in each iteration over and over again, which means only the last iteration determines what the content will be. 迭代数组时,您将一遍又一遍地设置每次迭代中完全相同的元素的内容,这意味着只有最后一次迭代才能确定内容是什么。 You should put each text in the proper element, avoiding to overwrite the previous written value. 您应该将每个文本放在适当的元素中,以避免覆盖先前的写入值。
  • The HTML you have shared, only has space for writing the result of the first array element to -- there should be similar elements for the second row, third row, etc. 您共享的HTML仅具有用于将第一个array元素的结果写入的空间-第二行,第三行等应该有相似的元素。

To solve all this, you could get inspiration from this code: 为了解决所有这些问题,您可以从以下代码中获得启发:

$.ajax({
    type: "GET",
    url: "my url" + x,
    dataType: "json", // corrected spelling and type
    success: function(data){
        // Don't parse as JSON string any more, data alread is object;
        // Don't name your variable jsonArray, it is neither of those
        var result = data.result;
        var jsonArray3 = result.oilFilter;
        $.each(jsonArray3, function (i1, js2) {
            var proname = js2.productName;
            var mrp = js2.mrp;
            var deliveryStatus = js2.deliveryStatus;
            var oilid = js2.productId;
            // bind onclick handler with oilid argument bound to it:
            // use `get()` to select the next element of the same class:
            $(".oilid").get(i1).off('click').on('click', ajax_cross.bind(null, oilid));
            // use text(), not html()
            $(".deliveryStatusOil").get(i1).text(deliveryStatus);
            $(".mrpoil").get(i1).text(mrp);
            $(".oilprodetail").get(i1).text(proname);
        });
    }
});

<label id="oilprodetail" class="oilprodetail"></label><br/>
<label id="mrpoil" class="mrpoil"></label><br/>
<!-- remove onclick attribute, we bind the handler dynamically -->
<a id="oilid" class="oilid" >View</a>

<hr><!-- repeat elements for second row -->
<label id="oilprodetail" class="oilprodetail"></label><br/>
<label id="mrpoil" class="mrpoil"></label><br/>
<!-- remove onclick attribute, we bind the handler dynamically -->
<a id="oilid" class="oilid" >View</a>

<hr><!-- repeat elements for third row -->
<label id="oilprodetail" class="oilprodetail"></label><br/>
<label id="mrpoil" class="mrpoil"></label><br/>
<!-- remove onclick attribute, we bind the handler dynamically -->
<a id="oilid" class="oilid" >View</a>

The above is still not ideal, as you would better create the elements dynamically as you iterate through the Ajax results, so you would have just as many as you would need. 上面的方法还是不理想的,因为您最好在遍历Ajax结果时动态地创建元素,因此您将拥有所需的数量。

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

相关问题 如何在不更改元素的子元素的情况下更改元素的文本? - How can I change an element's text without changing its child elements? 如何在不影响其子元素的情况下更改元素的文本? - How can I change the text of an element without affecting its child element? 如何在不更改子元素的情况下更改元素 innerText - How do you change an element innerText without changing its children 如何在不使用jQuery更改html的情况下更改元素的同级文本? - How to change sibling text of element without changing html using jQuery? 更改元素的文本而不更改子元素 - change text of an element without changing child elements 如何在不影响元素的 rest 和任何事件侦听器的情况下覆盖元素文本的一部分 - How can I overwrite a portion of an element's text without affecting the rest of the element and any event listeners 更改文本时如何更新 Note 元素? - How can I get my Note element to update when I change its text? 如何更改<span>元素</span>的文本<span>?</span> - How can I change the text of a <span> element? 如何使用jQuery更改元素中的文本? - How can I change the text in element with jQuery? 如何在不更改当前页面的情况下更改特定目录? - How can I change a specific directory without changing the current page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM