简体   繁体   English

javascript - 如何从具有相同名称的输入(数组)中获取值

[英]javascript - how to get value from input with same name (array)

I want to use billingname[] and billingcity[] instead but I don't know how to write .value for these input.我想改用billingname[]billingcity[]但我不知道如何为这些输入编写.value (Now I use ordinal number such as billingname1, billingname2 but I don't want ordinal number) (现在我使用序号,例如 billingname1, billingname2 但我不想要序号)

<script>
    function FillBilling(f) {
      if(f.billingtoo1.checked == true) {
        f.billingname1.value = f.shippingname.value;
        f.billingcity1.value = f.shippingcity.value;
      }
        if(f.billingtoo1.checked == false) {
        f.billingname1.value = '';
        f.billingcity1.value = '';
      }

      if(f.billingtoo2.checked == true) {
        f.billingname2.value = f.shippingname.value;
        f.billingcity2.value = f.shippingcity.value;
      }
        if(f.billingtoo2.checked == false) {
        f.billingname2.value = '';
        f.billingcity2.value = '';
      }
    }
    </script>



    <td bgcolor="eeeeee">
    <b>Mailing Address</b>
    <br><br>
    <form id="add_field">
    Name:
    <input type="text" name="shippingname">
    <br>
    City:
    <input type="text" name="shippingcity">
    <br>
    <input type="checkbox" onclick="FillBilling(this.form)" name="billingtoo1">
    <em>Check this box if Billing Address and Mailing Address are the same.</em>
    <p>
    <b>Billing Address</b>
    <br><br>
    Name:
    <input type="text" name="billingname1">
    <br>
    City:
    <input type="text" name="billingcity1">
    </p>

    <input type="checkbox" onclick="FillBilling(this.form)" name="billingtoo2">
    <em>Check this box if Billing Address and Mailing Address are the same.</em>
    <p>
    <b>Billing Address</b>
    <br><br>
    Name:
    <input type="text" name="billingname2">
    <br>
    City:
    <input type="text" name="billingcity2">
    </p>
    </form>
    </td>

If you're not going to give each field a unique identificator (maybe a id property), you have to iterate over all of them in order to get the value for each one:如果你不打算给每个字段一个唯一的标识符(可能是一个id属性),你必须遍历所有字段以获取每个字段的值:

$('input[name="billingname[]"]').each(function() { 
    this.value = f.shippingname.value; 
});

Not sure how the code actually works on your environment, but it should give you an idea.不确定代码实际上如何在您的环境中工作,但它应该给您一个想法。

暂无
暂无

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

相关问题 如何使用 JavaScript 从具有相同名称但具有不同类型的数组中获取一个字符串/值? - how to get one string / value from an Array which has the same name but has a different type using JavaScript? 从多个具有相同名称的文本框中获取带有javascript的输入框的值 - Get value of input box with javascript from multiple text boxes with same name 如何获取与javascript数组同名的input元素的值? - How do I get values of input element with the same name as an javascript array? 如何在javascript中使用名称值获取数组编号 - How to get the array number with the name value in javascript 如何在反应js中获取具有相同属性名称的数组中输入标签的值 - how to get value of input tag in array with same attribute name in react js 如何从输入中获取@name的数组索引? - How to get the array index of an @name from an input? 如何使用JavaScript获取名称为数组的输入字段的值 - How to get value of input field which is an array, by it's name using javascript 从具有相同名称的多个输入类型 =“文本”中获取值 - Get value from multiple input type=“text” with same name 如何从多个具有相同名称的输入文本字段中仅获取一个值? - How to get only one value from multiple input text field with same name? 如何使用jquery从具有相同类名的输入中获取最大值 - how to get the max value from an input of having same class name using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM