使用jQuery将多个输入字段的多个值复制到多个输入字段中?(copy Multiple values from multiple input fields into multiple input fields using jQuery?)

我可以将多个输入字段复制到其他输入字段,但我认为它可以更好。 现在我使用下面的代码,但必须有更好的代码。 请帮助我。

$(document).ready(function() {
  $("#property_year_built").change(function() {
    $("#bouwjaar_field").val($('#property_year_built').val());
  });
});

$(document).ready(function() {
  $("#property_zip").change(function() {
    $("#postcode_field").val($('#property_zip').val());
  });
});

$(document).ready(function() {
  $("#property_price").change(function() {
    $("#koopprijs_field").val($('#property_price').val());
  });
});

I can copy multiple input fields into other input fields, but I think it can be better. Now I'm using the following code, but there has to be a better code. Please help me out.

$(document).ready(function() {
  $("#property_year_built").change(function() {
    $("#bouwjaar_field").val($('#property_year_built').val());
  });
});

$(document).ready(function() {
  $("#property_zip").change(function() {
    $("#postcode_field").val($('#property_zip').val());
  });
});

$(document).ready(function() {
  $("#property_price").change(function() {
    $("#koopprijs_field").val($('#property_price').val());
  });
});

                

最满意答案

在我的示例中,将一些css类添加到您希望监视“SomeCssClassOnAllTxt”的所有输入中。 在我的情况下,为所有6个输入添加新的属性它是“someAttribute”。 输入与“#property_year_built”和“#bouwjaar_field”输入相同的属性值。 并对其他输入进行相同的更改...

$(document).ready(function() { $(".SomeCssClassOnAllTxt").change(function() { var attributeValue = $(this).attr('someAttribute'); $("[someAttribute='+attributeValue+']") .not(".SomeCssClassOnAllTxt") .val($(this).val()); }); });

更新:

$(document).ready(function() { $("#property_year_built").change(function() { $("#bouwjaar_field").val($('#property_year_built').val()); }); $("#property_zip").change(function() { $("#postcode_field").val($('#property_zip').val()); }); $("#property_price").change(function() { $("#koopprijs_field").val($('#property_price').val()); }); });

Add some css class to all you inputs that you would like to monitor "SomeCssClassOnAllTxt" in my example. Also add new attribute to all your 6 inputs in my case it is "someAttribute". Type the same attribute value to "#property_year_built" and "#bouwjaar_field" inputs. And make the same changes for other inputs...

$(document).ready(function() { $(".SomeCssClassOnAllTxt").change(function() { var attributeValue = $(this).attr('someAttribute'); $("[someAttribute='+attributeValue+']") .not(".SomeCssClassOnAllTxt") .val($(this).val()); }); });

UPDATED:

$(document).ready(function() { $("#property_year_built").change(function() { $("#bouwjaar_field").val($('#property_year_built').val()); }); $("#property_zip").change(function() { $("#postcode_field").val($('#property_zip').val()); }); $("#property_price").change(function() { $("#koopprijs_field").val($('#property_price').val()); }); });使用jQuery将多个输入字段的多个值复制到多个输入字段中?(copy Multiple values from multiple input fields into multiple input fields using jQuery?)

我可以将多个输入字段复制到其他输入字段,但我认为它可以更好。 现在我使用下面的代码,但必须有更好的代码。 请帮助我。

$(document).ready(function() {
  $("#property_year_built").change(function() {
    $("#bouwjaar_field").val($('#property_year_built').val());
  });
});

$(document).ready(function() {
  $("#property_zip").change(function() {
    $("#postcode_field").val($('#property_zip').val());
  });
});

$(document).ready(function() {
  $("#property_price").change(function() {
    $("#koopprijs_field").val($('#property_price').val());
  });
});

I can copy multiple input fields into other input fields, but I think it can be better. Now I'm using the following code, but there has to be a better code. Please help me out.

$(document).ready(function() {
  $("#property_year_built").change(function() {
    $("#bouwjaar_field").val($('#property_year_built').val());
  });
});

$(document).ready(function() {
  $("#property_zip").change(function() {
    $("#postcode_field").val($('#property_zip').val());
  });
});

$(document).ready(function() {
  $("#property_price").change(function() {
    $("#koopprijs_field").val($('#property_price').val());
  });
});

                

最满意答案

在我的示例中,将一些css类添加到您希望监视“SomeCssClassOnAllTxt”的所有输入中。 在我的情况下,为所有6个输入添加新的属性它是“someAttribute”。 输入与“#property_year_built”和“#bouwjaar_field”输入相同的属性值。 并对其他输入进行相同的更改...

$(document).ready(function() { $(".SomeCssClassOnAllTxt").change(function() { var attributeValue = $(this).attr('someAttribute'); $("[someAttribute='+attributeValue+']") .not(".SomeCssClassOnAllTxt") .val($(this).val()); }); });

更新:

$(document).ready(function() { $("#property_year_built").change(function() { $("#bouwjaar_field").val($('#property_year_built').val()); }); $("#property_zip").change(function() { $("#postcode_field").val($('#property_zip').val()); }); $("#property_price").change(function() { $("#koopprijs_field").val($('#property_price').val()); }); });

Add some css class to all you inputs that you would like to monitor "SomeCssClassOnAllTxt" in my example. Also add new attribute to all your 6 inputs in my case it is "someAttribute". Type the same attribute value to "#property_year_built" and "#bouwjaar_field" inputs. And make the same changes for other inputs...

$(document).ready(function() { $(".SomeCssClassOnAllTxt").change(function() { var attributeValue = $(this).attr('someAttribute'); $("[someAttribute='+attributeValue+']") .not(".SomeCssClassOnAllTxt") .val($(this).val()); }); });

UPDATED:

$(document).ready(function() { $("#property_year_built").change(function() { $("#bouwjaar_field").val($('#property_year_built').val()); }); $("#property_zip").change(function() { $("#postcode_field").val($('#property_zip').val()); }); $("#property_price").change(function() { $("#koopprijs_field").val($('#property_price').val()); }); });