weixin_33686714 2016-01-09 13:27 采纳率: 0%
浏览 14

Ajax发布空名称-固定

I have fallowing code parse json files from .json file and works well. when i give it name and trying to post from form data gives me empty post data.

HTML:

<div id="jewels" ></div>

JS:

<script type="text/javascript">

$.getJSON( "include/settings/guild_bank_jevels.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
   items.push( "<option  name='jewel_name' value='" + key + "'>" + val + "</option>" );
  });

  $( "<select/>", {

    html: items.join( "" )
  }).appendTo( "#jewels" );
});

</script>

Thanks in advance for help.

UPDATE Fixed

Script:

 <script type="text/javascript">
    $.getJSON( "include/settings/guild_bank_jevels.json", function( data ) {
    var items = [];

    $.each( data, function( key, val ) {
      items.push( "<option   value='" + key + "'>" + val + "</option>" );
    });

    $("#jewels").html(items);
    });
     </script>

HTML Part:

<select id="jewels" name="jewel_name"> 
</select>
  • 写回答

2条回答 默认 最新

  • weixin_33738578 2016-01-09 13:30
    关注

    You have some extra double quotes " in the following line :

    items.push( "<option  name="'jewel_name'" value='" + key + "'>" + val + "</option>" );
    ___________________________^____________^
    

    Please try to replace it by :

    items.push( "<option  name='jewel_name' value='" + key + "'>" + val + key + "</option>" );
    

    Snippet

    var items = [];
    var data={'first_key': 'first_val', 'second_key': 'second_val'};
    $.each( data, function( key, val ) {
      items.push( "<option  name='jewel_name' value='" + key + "'>" + val + "</option>" );
    });
    
    $( "<select/>", {
    
      html: items.join( "" )
    }).appendTo( "#jewels" );
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="jewels" ></div>

    Hope this helps.

    </div>
    
    评论

报告相同问题?