Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

jquery - 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. (Now I use ordinal number such as billingname1, billingname2 but I don't want ordinal number)

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

$('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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...