[英]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: 您的代码有几个问题:
datatype
, which should be dataType
; datatype
,应该是dataType
; 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
参数。 .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(...)
; a
element does not have a value
property, so calling .val()
on it does not have the desired effect; a
元素没有value
属性,因此在其上调用.val()
并没有达到预期的效果。 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
),而不是值。 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.