最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I show all items in array in alert box? - Stack Overflow

programmeradmin4浏览0评论

Here's a snippet of my code:

onclick : function(){           // default button function 
                           form = new Object(); 
                           field = new Object();
                           form.vald = 0;
                           field.first = $('input[name$="sf_name"]');
                           field.last = $('input[name$="sf_last"]');
                           field.title = $('input[name$="sf_title"]');
                           fieldpany = $('input[name$="sf_pany"]');
                           field.email = $('input[name$="sf_email"]');
                           field.phone = $('input[name$="sf_phone"]');
                           field.key = $('input[name$="sf_key"]');
                           fields = new Array();

                           if (!field.first.val()) {

                               fields.push("First Name");

                           }
                           if (!field.last.val()) {
                               fields.push("Last Name")

                           }
                           if (!field.title.val()) {
                               fields.push("Title")

                           }
                           if (!fieldpany.val()) {
                               fields.push("Company")

                           }
                           if (!field.email.val()) {
                               fields.push("Email")

                           }
                           if (!field.phone.val()) {
                               fields.push("Phone")

                           }
                           console.log(fields);
                           if (form.valid == 0) {
                               window.location = "<?php bloginfo('url'); ?>/download?viewkey="+field.key.val();
                           } else {
                               alert('Please correct the following fields before continuing: ');
                               //$('#form_487940 input').css("box-shadow", "inset #BBB 0px 0px 5px");
                           }

As you can see any invalid fields gets pushed into an array, I need to show all the invalid fields in an alert box after 'Please correct the following fields before continuing:'.

Any help would be appreciated, maybe I could achieve the same result doing it a different way.

Here's a snippet of my code:

onclick : function(){           // default button function 
                           form = new Object(); 
                           field = new Object();
                           form.vald = 0;
                           field.first = $('input[name$="sf_name"]');
                           field.last = $('input[name$="sf_last"]');
                           field.title = $('input[name$="sf_title"]');
                           field.pany = $('input[name$="sf_pany"]');
                           field.email = $('input[name$="sf_email"]');
                           field.phone = $('input[name$="sf_phone"]');
                           field.key = $('input[name$="sf_key"]');
                           fields = new Array();

                           if (!field.first.val()) {

                               fields.push("First Name");

                           }
                           if (!field.last.val()) {
                               fields.push("Last Name")

                           }
                           if (!field.title.val()) {
                               fields.push("Title")

                           }
                           if (!field.pany.val()) {
                               fields.push("Company")

                           }
                           if (!field.email.val()) {
                               fields.push("Email")

                           }
                           if (!field.phone.val()) {
                               fields.push("Phone")

                           }
                           console.log(fields);
                           if (form.valid == 0) {
                               window.location = "<?php bloginfo('url'); ?>/download?viewkey="+field.key.val();
                           } else {
                               alert('Please correct the following fields before continuing: ');
                               //$('#form_487940 input').css("box-shadow", "inset #BBB 0px 0px 5px");
                           }

As you can see any invalid fields gets pushed into an array, I need to show all the invalid fields in an alert box after 'Please correct the following fields before continuing:'.

Any help would be appreciated, maybe I could achieve the same result doing it a different way.

Share Improve this question edited Sep 24, 2012 at 17:39 j08691 208k32 gold badges269 silver badges280 bronze badges asked Sep 24, 2012 at 17:38 user990717user990717 4709 silver badges18 bronze badges 1
  • for form.vald = 0; did you mean form.valid = 0? – Anoop Commented Sep 24, 2012 at 17:45
Add a ment  | 

5 Answers 5

Reset to default 4

use array.join like so:

alert('Please correct the following fields before continuing: \n' 
    + fields.join(", "));​​​​

would output a ma-separated list like

Please correct the following fields before continuing:

First Name, Last Name, Company

Try below,

alert('Please correct the following fields before continuing: ' + fields.join());
alert('Please correct the following fields before continuing: ' + fields.join(", "));

you can use join method of javascript array, but verify your code to be better ordered:

onclick: function() { // default button function 
    var
        form = {
           vald: true
        },
        field = {
            'first': $('input[name$="sf_name"]'),
            'last': $('input[name$="sf_last"]'),
            'title': $('input[name$="sf_title"]'),
            'pany': $('input[name$="sf_pany"]'),
            'email': $('input[name$="sf_email"]'),
            'phone': $('input[name$="sf_phone"]'),
            'key': $('input[name$="sf_key"]')
        },
        fields = [];

    var addValue = function(el, value) {
        if (!el.val()) {
            form.valid = false;
            fields.push(value);
        }
    };

    addValue(field.first, "First Name");
    addValue(field.last, "Last Name");
    addValue(field.title, "Title");
    addValue(field.pany, "Company");
    addValue(field.email, "Email");
    addValue(field.phone, "Phone");

    if (form.valid) {
        //not remended to do this, for example could be an input value
        window.location = "<?php bloginfo('url'); ?>/download?viewkey=" + field.key.val();
        //remended  
        //window.location = $('#url').val() + '/download?viewkey=' + field.key.val();

        return;
    }

    var fieldsString = field.join(" \n ");
    alert("Please correct the following fields before continuing: \n" + fieldsString);

}

Or:

onclick: function() { // default button function 
    var
        field = {
            'First Name': $('input[name$="sf_name"]'),
            'Last Name': $('input[name$="sf_last"]'),
            'Title': $('input[name$="sf_title"]'),
            'Company': $('input[name$="sf_pany"]'),
            'Email': $('input[name$="sf_email"]'),
            'Phone': $('input[name$="sf_phone"]'),
            'key': $('input[name$="sf_key"]')
        },
        fields = [];

    $.each(field, function(i, v) {
        if (i != 'key' && !v.val()) {
            fields.push(i);
        }
    };

    if (fields.length == 0) {
        window.location = $('#url').val() + '/download?viewkey=' + field.key.val();
        return;
    }

    var fieldsString = field.join(" \n ");
    alert("Please correct the following fields before continuing: \n" + fieldsString);
}

Try this

var field = ['Name Invalid', 'Email Invalid' , 'Phone Invalid' 
              , 'Address Invalid'];​​​​​
var alertStr = '';
$.each(field , function(i) {
    alertStr += field[i] + '\n'
 })

 alert(alertStr)   

Check FIDDLE

​ This will make sure each error is in a separate line

If that's the case you can also use

fields.join('\n');
发布评论

评论列表(0)

  1. 暂无评论