Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript从单击事件中的“.row”中获取选择字段的值_Javascript_Jquery_Asp.net Mvc_Events_Jquery Events - Fatal编程技术网

使用Javascript从单击事件中的“.row”中获取选择字段的值

使用Javascript从单击事件中的“.row”中获取选择字段的值,javascript,jquery,asp.net-mvc,events,jquery-events,Javascript,Jquery,Asp.net Mvc,Events,Jquery Events,我有一个用户文档记录,在其中动态添加或删除行并控制它们的索引 每一行都位于名为div documentos的div中。我创建了两个事件:click事件用于存储每行的select document tipo的值,change事件用于在用户选择select中的选项后执行操作。 问题在于,单击事件无法从相应行获取字段select的值,也就是说,它带来的值来自与当前行不同的另一行 var documentoTipoIdPrevious = "0"; $("#div-documentos").bind("

我有一个用户文档记录,在其中动态添加或删除行并控制它们的索引

每一行都位于名为div documentos的div中。我创建了两个事件:click事件用于存储每行的select document tipo的值,change事件用于在用户选择select中的选项后执行操作。 问题在于,单击事件无法从相应行获取字段select的值,也就是说,它带来的值来自与当前行不同的另一行

var documentoTipoIdPrevious = "0";
$("#div-documentos").bind("click", ".sel-documento-tipo", function (e) {
    //get current row select value
    //is not working
    documentoTipoIdPrevious = $(this).closest('.row').find('.sel-documento-tipo').val();
    alert(documentoTipoIdPrevious)

}).on("change", ".sel-documento-tipo", function (e) {
//it's working  
});
怎么了?为什么要从另一行获取select值

HTML:


用纯HTML检查代码会更容易

我相信我发现了你的问题

您希望鼠标向下移动并在“选择”按钮上进行更改。 无需导航到.row以查找您正在与之交互的选定项 两个项目我都改为.on var documentoTipoIdPrevious=0; $div-documentos.onmousedown、.sel documento tipo、functione{ documentotipodprevious=$this.val; console.logBefore,documentotipodprevious }.onchange、.sel documento tipo、function e{ console.logAfter$this.val }; 特图罗·德埃利奥特 提波文件 CPF-佩索阿国家电力公司 RG-注册GERAL RNE-国家隔阂登记处 特图罗·德埃利奥特 CTPS-社会福利机构 CNH-卡泰拉民族健身俱乐部 CAM-军事认证 帕萨波特 NIS-NÚMERO DE IDENTIFICAÃO SOCIAL ×中央公积金 -佩索阿费西卡国家酒店 努姆。文件
好吧,这是问题所在,是你的问题

$("#div-documentos").bind("click", ".sel-documento-tipo", function (e) {
    //get current row select value
    //is not working
    documentoTipoIdPrevious = $(e.target).val();
    alert(documentoTipoIdPrevious)

}).on("change", ".sel-documento-tipo", function (e) {
//it's working  
});

为什么?绑起来,穿上。bind被.on替换,不推荐使用此选项与ASP无关。如果您改为单击编辑,然后单击[]代码段编辑器并仅发布JS和HTML,那么我们可以测试您的代码,为什么您不使用$this.val而不是$this.closest'.row'.find'.sel documento tipo'.valHello@mplungjan!只有更改事件起作用。我不知道还能做什么:@MasterJR我用你的HTML更新了。我的代码在您请求时工作EDI使用生成的html更新帖子。如果仅使用$this.val;,则无法获取值并选择;。您可以使用$this.closest'获取值。行'。找到'。选择“蒂波”。Val,但它不是当前行的值。。。它看起来像是丢失了获取值…当您委托给select时,不需要使用e.target。也不推荐使用bind。请看我的回答Hello@mplungjan。mousedown事件不起作用。。。当我从“绑定”更改为“活动”时,该事件不起作用。是否有人可以远程访问我的电脑并进行检查?所以我们可以把答案留在这里。我什么都试过了…:你有没有像我在更新中提到的那样尝试使用e.target?
$("#div-documentos").bind("click", ".sel-documento-tipo", function (e) {
    //get current row select value
    //is not working
    documentoTipoIdPrevious = $(e.target).val();
    alert(documentoTipoIdPrevious)

}).on("change", ".sel-documento-tipo", function (e) {
//it's working  
});