所以,我有这个代码,它可以工作:
var curp = document.getElementById("id_sc_field_curp_id_1");
var getcurp = curp.options[curp.selectedIndex].text;
var rfc = getcurp.substr(0, 10);
document.getElementById("id_sc_field_virtual_rfc_1").value = rfc;它复制字段(td - CURP)“RFC”中的文本,并对其进行修剪以将结果放入另一个字段(RFC) "id_sc_field_virtual_rfc_1“中。
JSFIDDLE:https://jsfiddle.net/90yzgcqe/1/
我想调整代码以与其他行一起工作,这些行具有增量id…id_sc_field_curp_id_1、id_sc_field_curp_id_2、id_sc_field_curp_id_3、d_sc_field_virtual_rfc_1、d_sc_field_virtual_rfc_2、d_sc_field_virtual_rfc_3...etc
我在做这个函数,但是...我不知道怎么让它工作。
function rfc() {
for (var i = 0; i <= 19; i++) {
var curp = document.getElementById("id_sc_field_curp_id_" + i);
var getcurp = curp.options[curp.selectedIndex].text;
var rfc = getcurp.substr(0, 10);
document.getElementById("id_sc_field_virtual_rfc_" + i).value = rfc;
}
}怎么啦?
发布于 2018-08-02 03:49:45
一些jQuery可以很容易地让我们到达那里,首先获得匹配的下拉列表,然后与它们交互。
$(function() {
//get the list of dropdowns that start with all but the numeral
var lst = $("[id^='id_sc_field_curp_id_']");
$.each(lst, function(idx, elem) {
//lets store the dropdown for use in the loop
let $field = $(elem);
//for example lets print the selected text
console.log($field.find("option:selected").text());
});
});有几个选项,你可以使用下拉菜单来创建rfc的id,或者使用jQuery函数closest()来获取它。一旦有了相关的rfc输入,设置这个值就很容易了。
编辑:1个
更具体的javascript,以及指向修改后的jsFiddle的链接
$(function() {
//get the list of dropdowns that start with all but the numeral
var lst = $("[id^='id_sc_field_curp_id_']");
$.each(lst, function(idx, elem) {
//lets store the dropdown for use in the loop
let $field = $(elem);
//for example lets alert the selected text
alert($field.find("option:selected").text().substr(0,10));
$field.closest("[id^='idVertRow']")
.find("[id^='id_sc_field_virtual_rfc_']")
.val($field.find("option:selected").text().substr(0,10));
});
});https://stackoverflow.com/questions/51640741
复制相似问题