Jquery 编辑:如何检查JSON的值,并将其与单选按钮的值进行比较,以便选择它

Jquery 编辑:如何检查JSON的值,并将其与单选按钮的值进行比较,以便选择它,jquery,html,json,Jquery,Html,Json,我有一个表单,在打开HTML文件时,它将读取JSON值并显示到表单中。我需要检查文本框是否与Jollibee和Mcdo两个选项相同 如果相同,它将自动选中与文本框具有相同值的两个选项中的任何一个,同时它将禁用文本框,如果不相同,它将选择其他选项,值将相同。顺便说一句,我是从JSON获取我的值的 这是我在JSFIDLE中的代码,但我无法在JSFIDLE中使用它,但使用记事本++它可以很好地工作。。请随意编辑,非常感谢 HTML JQUERY $(document).ready(function()

我有一个表单,在打开HTML文件时,它将读取JSON值并显示到表单中。我需要检查文本框是否与Jollibee和Mcdo两个选项相同

如果相同,它将自动选中与文本框具有相同值的两个选项中的任何一个,同时它将禁用文本框,如果不相同,它将选择其他选项,值将相同。顺便说一句,我是从JSON获取我的值的

这是我在JSFIDLE中的代码,但我无法在JSFIDLE中使用它,但使用记事本++它可以很好地工作。。请随意编辑,非常感谢

HTML

JQUERY

$(document).ready(function()
{
   $('#name').val(info['name']);
   $("#age").html(theAge);
   $("#age").val(info['age']);
   $('#otherfastfood').val(info['fastfood']);
   $('input[name="fastfood"][value="Other"]').prop("checked",true);
   $("#favorite_flavor").html(theFlavor);
   $("#favorite_flavor").val(info['flavor']);
   $("#house-1").val(info['houses'][0]);
   $("#house-2").val(info['houses'][1]);

   $('input[type=radio]').change(function()
   {
     if ($(this).is(':checked')) 
     { 
        if(info['fastfood'])
        {
            $('input[id=otherfastfood]').attr('disabled', 'disabled');
            $(this).next().removeAttr('disabled');
            $('#otherfastfood').val("");
        }
    }
});

$('#hBtn').click(function()
{
    Houses();
});

$("#myBtn").click(function()
{
    var series = {};
    var h = [];
    var counter = $("[id^=house]").length;
    series.name = $('#name').val();
    series.age = [parseInt($('#age').val())];

    if ($('input[value=Jollibee]').is(':checked')) 
        series.fastfood = $('input[value=Jollibee]').val();

    if ($('input[value=Mcdo]').is(':checked')) 
        series.fastfood = $('input[value=Mcdo]').val();

    if ($('input[value=Other]').is(':checked')) 
        series.fastfood = $('#otherfastfood').val();

    series.flavor = $('#favorite_flavor').val();

    for (var i = 1; i <= counter; i++) 
    {
        h.push($('#house-' + i).val());
    }
    series.houses = h;
    alert(JSON.stringify(series));
    });
});

var info = JSON.parse('{"name":"Juan dela Cruz", "age":29, "fastfood":"Jollibee", "houses": ["Manila City", "Paranaque City"], "flavor":"mango", "set_flavors":["ube","matcha","chocolate","mocha","mango"]}');
var counter = 3;
var str = '';

function Houses() 
{
  str = '<div>'+counter+'. <select id="house-'+counter+'"><option>Quezon City</option><option>Makati City</option><option>Manila City</option><option>Paranaque City</option></select></div></br>';
  $('#theHouses').append(str);
  counter++;
}

function theAge()
{
  var agesRange = "";

  for(var a = 1; a <= 100; a++)
  {
    agesRange += '<option>'+a+'</option>';
  }

  return agesRange;
 } 

function theFlavor()
{
   var flavors = "";
   var counter = info['set_flavors'].length;

   for(var a = 0; a < counter; a++)
   {
     flavors += '<option>'+info['set_flavors'][a]+'</option>';
   }

   return flavors;
}
检查JSFIDLE:

在文本框中检查文本Jollibee

 $(".nameStr").on('change', function() {
    if($(this).val() == "Jollibee"){
        $('.Jollibee').prop('checked', true);
    }
});

邮政编码有问题,谢谢!同时给出JSON.class的文本框。我已经加上了。您也可以使用id名称来完成此操作。sirI,我认为您的其他代码中存在错误。它的未捕获类型错误:无法读取未定义的属性“name”,因此请使用$'name.val;代替$'name'.valinfo['name'];是的,我已经试过了,我应该删除$'otherfastfood'。valinfo['fastfood';因为它显示在文本框Jollibee中
 $(".nameStr").on('change', function() {
    if($(this).val() == "Jollibee"){
        $('.Jollibee').prop('checked', true);
    }
});
$('input[value=Other]').click(function(){
    if($(this).prop('checked')){
        $('#otherfastfood').prop('disabled',false)
    }
});

$('#otherfastfood').on('change',function(){
    $('input[name=fastfood]').each(function(custom){
       if($(this).val()!='Other' && $(this).val().toLowerCase()==$('#otherfastfood').val().toLowerCase()){
            $(this).prop("checked",true);   
            $('#otherfastfood').val('');
            $('#otherfastfood').prop('disabled',true)
        }
    });
});