gpt4 book ai didi

javascript - 将所选值与另一个列表中的值进行比较

转载 作者:行者123 更新时间:2023-11-28 05:49:10 25 4
gpt4 key购买 nike

我想要做的是将下拉列表中选择的值目标到数据结果列表。在有这些下拉列表中:

 var body = String.format("{{'__metadata':{{'type':'{0}'}},'Nombre':'{1}','Empresa':'{2}','Correo':'{3}','Area':'{4}','Telefono':'{5}','Mensaje':'{6}'}}",
GetItemTypeForListName('Contacto'), jQuery("#txtNombreCompleto2").val(), jQuery("#txtNombreEmpresa").val(),jQuery("#exampleInputEmail2").val(),$('#dropOficina2').val(),jQuery("#txtTelefono2").val(),jQuery("#txtMensaje2").val());

我的 $('#dropOficina2').val() 有 3 个值:“Finanzas”、“Operacion”和“Recursos Humanos”因此,根据选择的值,它会将一个值返回到正文中

我得到如下标题和 Correo 列表:

   var query = "?$select=Title,Correo";
$.ajax({
url: url + "/_api/web/lists/getbytitle('ContactosCorreos')/items" + query,
method: 'GET',
headers: { "Accept": "application/json; odata=verbose" },
success: function(data) {
for (var i = 0; i < data.d.results.length; i++) {
console.log("Title: "+data.d.results[i].Title);
console.log("Correo: "+data.d.results[i].Correo); }
}});

所以我在标题中得到了我想要匹配的 3 个值:“Finanzas”、“Operacion”和“Recursos Humanos”其中每一项都有不同的值,例如 Finanzas=finanzas@hotmail.com

因此,在下面的“to”变量中,我想要执行以下操作:如果用户在下拉列表中选择“Finanzas”,它将在标题列表中查找“Finanzas”,并返回所选下拉列表的“Correo”值

  jQuery.ajax({
url: url + "/_api/web/lists/getbytitle('Contacto')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: body,
async:false,
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
var from = "xxx.hotmail.com",
to = "", //there is that I want to change depending of selected dropdown value
body = "Mail enviado",
subject ="Hay un nuevo Contacto!!";

var urlTemplate = url + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
async:false,
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': from,
'To': { 'results': [to] },
'Body': body,
'Subject': subject
}
}
),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert("Gracias por contactarnos!");
window.location= siteurl+"/SitePages/Home.aspx"
},
error: function (err) {
console.log(err);
alert("Error al enviar el correo");
}
});

我尝试这些:但它总是向我发送“Title”和“Correo”数组

  var selectedvalue = $("#dropOficina2").val();
var to="";
var query = "?$select=Title,Correo";
$.ajax({
url: url + "/_api/web/lists/getbytitle('ContactosCorreos')/items" + query,
method: 'GET',
headers: { "Accept": "application/json; odata=verbose" },
success: function(data) {
for (var i = 0; i < data.d.results.length; i++) {
console.log("Title: " + data.d.results[i].Title);
console.log("Correo: " + data.d.results[i].Correo);
}
if (selectedvalue === "Finanzas") {
to = data.d.results.Correo;
} else if (selectedvalue === "Operación") {
to = data.d.results.Correo;
} else if (selectedvalue === "Recursos Humanos") {
to = data.d.results.Correo;
}

}});

最佳答案

设置“to”变量的条件语句应该放在 for 循环中:

var selectedvalue = $("#dropOficina2").val();
var to = "";
var query = "?$select=Title,Correo";
$.ajax({
url : url + "/_api/web/lists/getbytitle('ContactosCorreos')/items" + query,
method : 'GET',
headers : {
"Accept" : "application/json; odata=verbose"
},
success : function (data) {
for (var i = 0; i < data.d.results.length; i++) {
console.log("Title: " + data.d.results[i].Title);
console.log("Correo: " + data.d.results[i].Correo);
// move conditional statement into for loop
// check the value of item's title you really only need
//to have one condition if your Titles are unique
if (selectedvalue === data.d.results[i].Title) {
// access the results by index, just like you
//did in the console.log
to = data.d.results[i].Correo;
}
}
}
});

您可以通过在查询中的 selectedValue 上使用 $filter 来提高效率:

var query = "?$select=Correo&$filter=Title eq '" + selectedvalue +"'";

如果标题是唯一的,那么您只会获得一个 Correo 结果。

关于javascript - 将所选值与另一个列表中的值进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38229702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com