Javascript返回正确&未定义的值

时间:2017-08-25 13:29:16

标签: javascript jquery arrays

很抱歉,标题含糊不清,但我有一个表单可以接受多个ID并将用户输入的内容发布到网址,以根据帐户ID的数量打开x个标签页。

第一个帐户ID的链接是打开的,其中的值插入到表单中,但第二个帐户ID只显示“未定义”,并且从控制台看起来第二个帐户ID未超过第一个声明但跳过到第二个。

<form accept-charset="UTF-8" method="post"><div style="margin:0;padding:0;display:inline">
            <table class="vat-tax-processor center-table">
                <tbody>
                    <tr>
                        <td>
                            <span class="required">*</span>
                            AWS Account ID (No Dashes)<br>
                        </td>
                        <td><textarea autocomplete="off" id="AccountIDs" name="AccountIDs" value=""></textarea></td>
                        <tr>
                        <td>
                            <span class="required">*</span>
                            VAT Country <br>
                        </td>
                        <td><input autocomplete="off" type="text" value="" id="vatCountryCode" name="vatCountryCode"></td>
                        </tr>
                        <tr>
                        <td>
                            <span class="required">*</span>
                            Vat Number<br>
                        </td>
                        <td><input autocomplete="off" type="text" value="" id="vatNumber" name="vatNumber"></td>
                        </tr>
                        <tr>
                        <td>
                            <span class="required">*</span>
                            Registration Type <br>
                        </td>
                        <td><input autocomplete="off" type="text" value="Intra-EU" id="currentState" name="currentState"></td>
                        </tr>
                        <tr>
                        <td>
                            <span class="required">*</span>
                            Business Legal Name <br>
                        </td>
                        <td><input autocomplete="off" type="text" value="" id="businessLegalName" name="businessLegalName"></td>
                        </tr>
                        <tr>
                        <td>

这是我的js:

function addExemption(){
        var AccountIDsArray = $('textarea[id=AccountIDs]').val().split('\n');
        var vatCountryCodeArray = $('input[id=vatCountryCode]').val().split('\n');
        var vatNumberArray = $('input[id=vatNumber]').val().split('\n');
        /*var currentStateArray = $('input[id=currentState]').val().split('\n');*/
        var businessLegalNameArray = $('input[id=businessLegalName]').val().split('\n');
        console.log('I got past part 1 declarations - No issues here');

        $.each(AccountIDsArray, function(index, value){
            var AccountID = AccountIDsArray[index];
            var vatCountryCode = vatCountryCodeArray[index];
            var vatNumber = vatNumberArray[index];
            /*var currentState = currentStateArray[index];*/
            var businessLegalName = businessLegalNameArray[index];
            console.log('I got part part 2 declarations - No issues here either');
            console.log(AccountID);

            var URL = 'https://linkhere';

            var URL_Final = encodeURI(URL);

            window.open(URL_Final, '_blank');



        }
);

以下是第一个链接和第二个链接上显示内容的屏幕截图:

Account 1 Account 2

2 个答案:

答案 0 :(得分:0)

我看到有几个问题。

窗口在循环中打开,这意味着它会在第一个id打印到控制台后立即打开一个窗口。我会在$ .each之外移动window.open()。

你冒着越过vatCountryCodeArray,vatNumberArray,currentStateArray和businessLegalNameArray索引的风险,因为它们是输入元素而不是textareas。

'Undefined'你看是否因为你的函数没有返回值。 See here.

function addExemption(){
        var AccountIDsArray = $('textarea[id=AccountIDs]').val().split('\n');
        var vatCountryCodeArray = $('textarea[id=vatCountryCode]').val().split('\n');
        var vatNumberArray = $('textarea[id=vatNumber]').val().split('\n');
        var currentStateArray = $('textarea[id=currentState]').val().split('\n');
        var businessLegalNameArray = $('textarea[id=businessLegalName]').val().split('\n');
        console.log('I got past part 1 declarations - No issues here');

        $.each(AccountIDsArray, function(index, value){
            if(value != null && value != ''){
                var AccountID = value;
                //var vatCountryCode = vatCountryCodeArray[index];
                //var vatNumber = vatNumberArray[index];
                //var currentState = currentStateArray[index];
                //var businessLegalName = businessLegalNameArray[index];

                console.log('I got part part 2 declarations - No issues here either');
                console.log(AccountID);
            }
        });

        var URL = 'https://linkhere';

        var URL_Final = encodeURI(URL);

        window.open(URL_Final,'_blank');

        return '';
}

答案 1 :(得分:0)

我能够解决这个问题:

function addExemption(){


        var AccountIDsArray = $('textarea[id=AccountIDs]').val().split('\n');
        console.log('I got past 1st declarations - No issues here');


        $.each(AccountIDsArray, function(index, value){
            var AccountID = AccountIDsArray[index];
            var vatCountryCode = $('#vatCountryCode').val();
            var vatNumber = $('#vatNumber').val();
            var businessLegalName = $('#businessLegalName').val();
            console.log('I got past 2nd declarations - No issues here either');
            console.log(AccountID);

            var URL = 'https:xxxxx?accountId=' + AccountID + '&vatCountryCode=' + vatCountryCode + '&vatNumber=' + vatNumber + '&currentState=Intra-EU&businessLegalName=' + businessLegalName + '';

            var URL_Final = encodeURI(URL);

            window.open(URL_Final, '_blank');

        });

    }