首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Javascript影响多行substr和复制的函数

使用Javascript影响多行substr和复制的函数
EN

Stack Overflow用户
提问于 2018-08-02 03:12:50
回答 1查看 39关注 0票数 0

所以,我有这个代码,它可以工作:

代码语言:javascript
运行
复制
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“中。

Example img

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

我在做这个函数,但是...我不知道怎么让它工作。

代码语言:javascript
运行
复制
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;
  }
}

怎么啦?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-02 03:49:45

一些jQuery可以很容易地让我们到达那里,首先获得匹配的下拉列表,然后与它们交互。

代码语言:javascript
运行
复制
$(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的链接

代码语言:javascript
运行
复制
$(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));
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51640741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档