如何在多个下拉列表中使用相同的JavaScript函数

辅助1234

我有多个下拉列表动态添加,并且它们有自己的ID,我有一个函数,我在其中调用OnChange事件以根据基于下拉列表中的选择传递一些值,但此刻我只是使用第一个选择进行此操作。

JS:

$("#id_detallepedido_set-0-producto").change(function () {
    producto_id = $(this).val();
    var url = '/control/price/'+producto_id;

    $.ajax({              
    type: "GET",    
    url: url,                   
    data: {
        'producto': producto_id 
    },
    success: function (data) {   
        console.log('Funciono!');
        console.log(data);
        $("#id_detallepedido_set-0-precio").val(data);       
    },
    error: function(data) {
        alert('error' + data);
        //console.log(data);
    } 

    });        
});

“ id_detallepedido_set-0-producto”是第一个Dropdownlist的ID,“ id_detallepedido_set-0-precio”是我根据所选值传递值的字段的ID,无论如何,在添加的每个下拉列表中都使用此功能动态地?

我在这里用Django Form添加这些下拉列表:

HTML:

<table class="table" id="tPedidos">
  {{ detalle.management_form }}
  {% for form in detalle.forms %}
    {% if forloop.first %}
      <thead>
        <tr>
            {% for field in form.visible_fields %}
                <th>{{ field.label|capfirst }}</th>
            {% endfor %}
        </tr>
        </thead>
    {% endif %}
      <tr class="formset_row-{{ formset.prefix }}">
        {% for field in form.visible_fields %}
          <td>
            {# Include the hidden fields in the form #}
            {% if forloop.first %}
                {% for hidden in form.hidden_fields %}
                    {{ hidden }}
                {% endfor %}
            {% endif %}
            {{ field.errors.as_ul }}
            {{ field }}
          </td>
        {% endfor %}
      </tr>
{% endfor %}
</table>
维沙尔莫迪

您可以使用class处理它,而不是使用id处理下拉更改事件。因此它将对所有下拉菜单执行。但是正如您所说的那样,下拉列表是动态添加的,您需要全局创建click事件。

假设您有3个下拉菜单,然后只需添加class =“ mydropdownclass”。并将class =“ mytextfield”添加到您的文本控件。

<table>
    <tr>
        <td>
            <select id="t1" class="mydropdownclass">
                <option value="1">101</option>
                <option value="2">102</option>
            </select>

        </td>
        <td>
            <input type="text" class="test" />
        </td>

        <td>
            <input type="text" class="mytextfield" />
        </td>
    </tr>

    <tr>
        <td>
            <select id="t2" class="mydropdownclass">
                <option value="1">101</option>
                <option value="2">102</option>
            </select>

        </td>
        <td>
            <input type="text" class="test" />
        </td>

        <td>
            <input type="text" class="mytextfield" />
        </td>
    </tr>
</table>

<script>
$(document).on("change", ".mydropdownclass" , function() {
    producto_id = $(this).val();
    var controlId = $(this).attr('id');

    //Updat textbox value
    $(this).closest("tr").find('.mytextfield').val($(this).val());

    var url = '/control/price/'+producto_id;

    $.ajax({              
    type: "GET",    
    url: url,                   
    data: {
        'producto': producto_id 
    },
    success: function (data) {   
        console.log('Funciono!');
        console.log(data);
        $("#"+controlId).val(data);       
    },
    error: function(data) {
        alert('error' + data);
        //console.log(data);
    } 

    });        

});

</script>

您可以检查更新的代码[链接]

https://jsfiddle.net/hubs3m0n/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在下拉列表中使用foreach循环?

如何在v8 Javascript中的多个函数中使用相同的上下文?

如何在React中使用api数据填充下拉列表?

如何在虚拟化列表中使用选择下拉列表?

如何在导航栏中使用多个下拉菜单?

如何使用Javascript获取选定的多个下拉列表中的值?

如何在CSHTML中使用angular创建下拉列表?

如何在Python中使用Selenium从站点下载多个文件以选择下拉列表的每个选项

如何在具有多个下拉列表的javascript中使用if语句?

如何在React中使用Formik创建动态下拉列表?

如何在$ .getJSON中使用下拉列表值?

如何在Kendo Grid mvvm中使用下拉列表

如何显示servlet在下拉列表中使用JavaScript输出

如何在JSON对象中使用下拉列表的更改值?

如何在JavaScript中的多个函数中使用Global变量?

如何在Emberjs中使用Bootstrap JavaScript插件(下拉列表,模式)

如何在AngularJS中使用表单创建下拉列表

如何在javascript中使用二维数组填充下拉列表

如何在 jquery 中使用多个下拉菜单

如何在r中使用带有多个参数的函数列表的应用函数?

在使用 javascript 和 ajax 时,如何在 forloop(laravel) 中使用许多动态下拉列表?

我如何在 javascript 中使用 this.value 从下拉列表中检查特定值

如何在 python 中使用 Selenium 包单击多个复选框和下拉列表?

如何在 $.each 函数中传递多个下拉列表的 id

如何在 SwiftUI 中使用 DisclosureGroup 作为下拉列表?

如何在 Vuetify 选择下拉列表中使用标签图标?

如何在具有相同类的多个 HTML div 上使用相同的 javascript 函数

如何在 python 中使用 selenium 选择下拉列表的项目

如何在 Flutter 的下拉列表中使用 API?