0

我想要做的是:如果用户在数据库中只有一个信用卡号码,那么在我成功完成的页面上显示文本字段但是如果用户的多个信用卡号码已存储在我的数据库中,那么我想显示所有而不是显示文本字段他的信用卡号码进入组合框。

以下是我得到的 ajax 响应:

{
    "fname": "abc",
    "lname": "xyz",
    "creditCardInfo": [
        {
            "creditCardNumber": "378282246310005",
            "creditCardType": "AX",
            "securityCode": "1234",
            "expirationDate": "2020-02-01"
        },
        {
            "creditCardNumber": "6011000990139424",
            "creditCardType": "DS",
            "securityCode": "321",
            "expirationDate": "2030-12-01"
        }
    ],
    "creditCardNumber": "6011000990139424",
    "creditCardType": "DS",
    "creditCardCVC": "321",
    "creditCardExpirationMonth": "12",
    "creditCardExpirationYear": "2030"
}

(注意:以上回复包含所有测试信用卡号和 cvc 代码)

我尝试使用 jqueryeach循环但能够做到。

以下是我的代码:

$.ajax({
type: "POST",
 url: "<?php echo $_SERVER['HOST']; ?>"+action,
 dataType: 'json',
 data: formdata,
 success: function(data)
{
var userinfo = eval(data);
alert(userinfo);
if(userinfo['msg'] == 'fail'){
$(".usrmsg").show();
$(".validate").hide();
}
else{

if(!$("#fname1").val()) $("#fname1").val(userinfo['fname']);
if(!$("#lname1").val()) $("#lname1").val(userinfo['lname']);
if(!$("#billingCity").val()) $("#billingCity").val(userinfo['billingCity']);

if(userinfo['creditCardInfo']){
$('#creditCardComboBox').show();
$('#creditCardTextField').show();

// $(userinfo['creditCardInfo']).each(function(index) {
// alert(index + ': ' + $(this).text());
// $("#creditCardNumber option[value="+index['creditCardNumber']+"]").attr("selected", "selected");                                
// });                                

$("#creditCardType option[value="+userinfo['creditCardInfo']['creditCardType']+"]").attr("selected", "selected");
$("#creditCardExpirationMonth option[value="+userinfo['creditCardInfo']['creditCardExpirationMonth']+"]").attr("selected", "selected");
$("#creditCardExpirationYear option[value="+userinfo['creditCardInfo']['creditCardExpirationYear']+"]").attr("selected", "selected");
}

if(!$("#billingFirstName").val()) $("#billingFirstName").val(userinfo['fname']);
if(!$("#billingLastName").val()) $("#billingLastName").val(userinfo['lname']);
if(!$("#creditCardCVC").val()) $("#creditCardCVC").val(userinfo['creditCardCVC']);
}
}
});

需要帮忙。

提前致谢。

4

1 回答 1

1

猜猜这将是你的代码。使用 jquery hide/show/ 使用 .innerHtml 标签来显示你的内容。

$.ajax({
  type: "POST",
  url: "<?php echo $_SERVER['HOST']; ?>"+action,
  dataType: 'json',
  data: formdata,
  success: function(data)
  {
  //Check the length of the json array
   var creditcardinfo = data.creditCardInfo

   do
   {
      if(creditcardinfo.length==1){
        $('#creditCardTextField').show();
        $('#creditCardComboBox').hide();
        //Set value of creditcardText
        $('#creditCardTextField').val(creditcardinfo[i].creditCardNumber);

      }else if(creditcardinfo.length>1){
        $('#creditCardComboBox').show();
        $('#creditCardTextField').hide();
        //ASSUMING YOU Are using <select> tag
        var newOption = $('<option>');
         newOption.attr('value',creditcardinfo[i].creditCardNumber).text(creditcardinfo[i].creditCardNumber);
        $('#creditCardComboBox').append(newOption);
      }
   }while (i<creditcardinfo.length);

  }
 });
于 2012-12-21T12:22:10.517 回答