【问题标题】:Javascript returning correct & undefined valueJavascript返回正确和未定义的值
【发布时间】:2017-08-25 13:29:15
【问题描述】:

抱歉,标题含糊不清,但我有一个表单,它接受多个 id 并将用户输入发布到 URL 以根据帐户 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

【问题讨论】:

  • 给我们输入,否则它只是猜测
  • 旁注:如果 id 是唯一的(它们应该是 ;)),使用 #id 而不是 ..[id=.. 构造更容易(也更有效)。例如$('#AccountIDs).val()
  • @Deckerz 输入来自我在上面添加的 html 表单。
  • @Me.Name 谢谢你 :) 有道理。

标签: javascript jquery arrays


【解决方案1】:

我看到了几个问题。

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

您有可能超出对 vatCountryCodeArray、vatNumberArray、currentStateArray 和 businessLegalNameArray 的索引,因为它们是输入元素而不是文本区域。

您看到的“未定义”是因为您的函数没有返回值。 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 '';
}

【讨论】:

  • 我应该提到被调用的 url 看起来像这样:var URL = 'https:/xxxxx?accountId=' + AccountID + '&amp;vatCountryCode=' + vatCountryCode + '&amp;vatNumber=' + vatNumber + '&amp;currentState=Intra-EU&amp;businessLegalName=' '; 所以你的代码 AccountID 没有被调用,因为我认为它在循环之外
【解决方案2】:

我能够像这样解决这个问题:

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');

        });

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 2021-02-26
    • 2013-04-23
    相关资源
    最近更新 更多