- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的 HTML 中有这个选择输入
<select class="" id="countries" name="Country">
<option value="AL">
Albania
</option>
<option value="HR">
Croatia
</option>
<option value="BG">
Bulgaria
</option>
<option value="MK">
Macedonia
</option>
<option value="MT">
Malta
</option>
<option value="MD">
Moldova
</option>
<option value="RO">
Romania
</option>
<option value="RS">
Serbia
</option>
<option value="SI">
Slovenia
</option>
</select>
在 Jquery 中,我有与此选择值同名的数组。数组中填充了不相关的数据。
var Albania = [];
var Croatia = [];
var Bulgaria= [];
...
根据选择的国家/地区,另一个选择输入应填充具有相同国家/地区名称的数组。另一个选择是 #cepsp
$( "#countries").change(function() {
$('#cepsp').empty();
var option = '';
for (var tt=0;tt<Albania.length;tt++)
{option += '<option value="'+Albania[tt]+'">'+Albania[tt]+'</option>';}
$('#cepsp').append(option);
});
请注意我是如何在 Jquery 中使用 Albania 数组的,它运行良好。但我也希望其他国家也这样做。我试过:
$( "#cepsp option:selected" ).text()[tt]
但这当然行不通。
如何将选定输入的文本传输到变量名称?
最佳答案
如果将数组结构更改为 an object thus :
var countriesArra ={
Albania:[],
Croatia:[],
Bulgaria:[],
....
}
您现在可以根据名称选择正确的数组:
$( "#countries").change(function() {
// get name that macthes array name, i.e. the text not the value
var countryName = $(this).text();
//countryName should be "Albania" not "AL"
$('#cepsp').empty();
var option = $('<option>');
for (var tt=0;tt<countriesArra[countryName].length;tt++)
{
//variable prevents querying the array twice, so this should be more efficent
var countryNameFromArra = countriesArra[countryName][tt];
option.val(countryNameFromArra).html(countryNameFromArra);
}
$('#cepsp').append(option);
});
这是有效的,因为你可以 access an object's properties by the string of the property name .
所以 countriesArra['Albania']
返回“阿尔巴尼亚”数组。然后你迭代你的 tt
在此数组上正常变量,例如countriesArra['Albania'][0]
返回对象的阿尔巴尼亚数组中的第一项。
我还整理了您的选项创建。所以我使用 jquery var option = $('<option>');
创建了一个“选项”对象.然后我可以访问它的方法和属性作为 jquery 对象,而不是字符串连接它。
option.val(countriesArra[countryName][tt]).html(countriesArra[countryName][tt]);
关于javascript - Jquery - 使用 HTML :selected to fetch an array with the same name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28833492/
我正在运行此代码并在没有互联网连接的情况下进行测试: fetch(url, options) .then(res => { // irrelevant, as catch happens
function fetchHandler(evt) { console.log('request:' + evt.request.url); dealWithRequest(evt)
我在 AdventureWorks2016 上执行了两个示例查询,并得到了相同的结果。那么什么时候应该使用 NEXT 或 FIRST 关键字? select LastName + ' ' + Firs
我有以下查询: @Query("SELECT new de.projectemployee.ProjectEmployee(employee) " + "FROM ProjectEmpl
我正在尝试使用 fetch on react 来实现客户端登录。 我正在使用护照进行身份验证。我使用的原因 fetch而不是常规 form.submit() , 是因为我希望能够从我的快速服务器接收错
我正在尝试将我的 Aurelia 项目从 beta 版本升级到 3 月版本。 我遇到的错误之一是: Cannot find name 'Request'. 谷歌搜索会在 GitHub 上显示此问题:h
见标题。在我们的react项目中调用fetch时,一位(现已离职)开发人员最初使用from fetch to window.fetch。我不确定两者之间的区别,也无法在网上找到任何结论(W3Schoo
这个问题在这里已经有了答案: HTTP status code 401 even though I’m sending credentials in the request (1 个回答) How
这是代码片段: var fetch = require("node-fetch"); var fetchMock = require("fetch-mock"); function setupMock
我在这里看到了两种不同的抓取方式: https://github.com/github/fetch https://github.com/matthew-andrews/isomorphic-fetc
以下git命令有什么区别? git fetch origin 和 git fetch --all 从命令行运行它们看起来就像它们做同样的事情。 最佳答案 git fetch origin 仅从 ori
我有一个不断改变值的动态 json。我想用该数据绘制图表所以我将动态数据存储到数组然后用该数组绘制图表。目前我创建了 serinterval 用于从 api 获取新数据。但问题是如果新数据没有,它会再
我有一个很大的 JSON blob,我想预先加载我的网页。为此,我添加了 到我的页面。我也有一个 JS 请求来获取相同的 blob。 这不起作用,控制台报告: [Warning] The resour
我们在单页 JavaScript 应用程序发出 fetch 请求时遇到不一致的客户端错误。值得注意的是,它们都是同源请求。 let request = new Request(url, options
我是 ReactJS 的新手,我一直在阅读如何从 api 获取和发布数据。我见过这两个,但我不知道该用什么以及两者之间有什么区别?我读了它,但我不确定我会用什么。谢谢! react-fetch wha
Doctrine中注解@ManyToOne中的fetch="EAGER"和fetch="LAZY"有什么区别? /** * @ManyToOne(targetEntity="Cart", casca
我想要获取一个 api,然后调用另一个 api。在 javascript 中使用这样的代码是否明智? fetch(url, { method: 'get', }).then(function(re
我有一个组件,它依赖于 2 个端点来检索所需程序的名称。我有 2 个端点。第一个端点返回程序列表,它是一个对象数组。目前,它仅返回 4 个节目(2 个节目 ID 为“13”,另外两个节目 ID 为“1
我的应用程序从外部源(配置文件)接收查询,因此它必须从查询结果中获取列。我有一些代码: typedef union _DbField { text text[512]; sword i
我有一个实体A,它与实体B有对多关系。 Entity A -->> Entity B 我需要在多个屏幕上引用一对多关系的计数。此外,我可以多次从 Entity A
我是一名优秀的程序员,十分优秀!