简体   繁体   English

对多个元素使用相同的功能

[英]Use same function on multiple elements

I need this function to work on multiple elements in the form, right now it only works on TfDiagnosis . 我需要此功能来处理表单中的多个元素,目前仅适用于TfDiagnosis How do I use it on TfDiagnosis and TfDiagnosis2 with results in TfSnowmed and TfSnowmed2 ? 我如何使用它TfDiagnosisTfDiagnosis2与结果TfSnowmedTfSnowmed2

JQUERY JQUERY

$(function snowmedlist() {
    $('#TfDiagnosis').on('click keyup change blur', function() {  
        if ($('#TfDiagnosis').val() == '[D]Anterograde amnesia (780.93)') {
            $('#TfSnowmed').val(206789002);
        }
        if ($('#TfDiagnosis').val() == '[D]Chills with fever (780.60)') {
            $('#TfSnowmed').val(206760004);
        }
    });
});

HTML 的HTML

<input name="TfDiagnosis" type="text" id="TfDiagnosis" size="100">
<input type="text" name="TfSnowmed" id="TfSnowmed">
<input name="TfDiagnosis2" type="text" id="TfDiagnosis2" size="100" >
<input type="text" name="TfSnowmed2" id="TfSnowmed2"></td>

It's easy to work on groups of elements using class names. 使用类名来处理元素组很容易。

<input name="TfDiagnosis" type="text" id="TfDiagnosis" class="diagnosis" size="100">
<input type="text" name="TfSnowmed" id="TfSnowmed">

js: js:

$('.diagnosis').on('click keyup change blur', function() {
    if($(this).val() == "...") {
        $(this).next().val(1.00);
    }
}) 

This way .next() is always the next element, so you don't need to keep passing IDs around. 这样, .next()始终是下一个元素,因此您无需一直传递ID。 You can then store the data outside of the function to get rid of a cluster of IF statements: 然后,您可以将数据存储在函数外部,以摆脱IF语句的簇:

var myData = []
myData['[D]Anterograde amnesia (780.93)'] = '206789002';
myData['[D]Chills with fever (780.60)'] = '206760004';

...then substitute the look-up from the array.... 然后替换数组中的查询

$('.diagnosis').on('click keyup change blur', function() {
        $(this).next().val(myData[$(this).attr(id)]);
}) 

You can use 您可以使用

$('#TfDiagnosis, #TfDiagnosis2').on('click keyup change blur', function() {  
     if($(this).attr('id') == 'TfDiagnosis' ){
           if ($(this).val() == '[D]Anterograde amnesia (780.93)') {
              $('#TfSnowmed').val(206789002);
           }
           if ($(this).val() == '[D]Chills with fever (780.60)') {
              $('#TfSnowmed').val(206760004);
           } 
       }else{
           //Stuff to do in case it is the #TfDiagnosis2
       }
    });

The most efficient way to make your function work on multiple inputs is to use event delegation: 使函数在多个输入上工作的最有效方法是使用事件委托:

$(document).on('click keyup change blur', 'input', function() {
    var value = $(this).val(); //Get the value only once 

    if (value == '[D]Anterograde amnesia (780.93)') {
        $('#TfSnowmed').val(206789002);
    }
    else if (value == '[D]Chills with fever (780.60)') {
        $('#TfSnowmed').val(206760004);
    }
});

Which will call the function for any input on the page. 它将为页面上的任何输入调用该函数。 You probably want to assign a class to the specific inputs you want to use like so: 您可能想要为要使用的特定输入分配一个类,如下所示:

HTML 的HTML

<input name="TfDiagnosis" type="text" id="TfDiagnosis" class="TfInput" size="100">
<input type="text" name="TfSnowmed" id="TfSnowmed" class="TfInput">
<input name="TfDiagnosis2" type="text" id="TfDiagnosis2" class="TfInput" size="100" >
<input type="text" name="TfSnowmed2" id="TfSnowmed2" class="TfInput">

JavaScript 的JavaScript

$(document).on('click keyup change blur', '.TfInput', function() {
    var value = $(this).val(); //Get the value only once 

    if (value == '[D]Anterograde amnesia (780.93)') {
        $('#TfSnowmed').val(206789002);
    }
    else if (value == '[D]Chills with fever (780.60)') {
        $('#TfSnowmed').val(206760004);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM