问:
How to get all radio button value into Json and load using local storage?

$(document).ready(function() {

  $('#showme').click(function() {
    //validateNresult() ;
    exportResultJson();
  })

  $('.allrb').change(valueE);

});

function validateNresult() {

  var firstInvalidRowIndex = 0;
  $.each($("tbody tr"), function(i, e) {
    var hasInvalidInputs = $(this).find(".numFr, .numIr").filter(function() {
      return $.trim(this.value).length === 0;
    }).length > 0;

    if (hasInvalidInputs) {
      $(this).addClass("invalid");
      if (firstInvalidRowIndex <= 0) {
        firstInvalidRowIndex = i + 1;
      }
    } else {
      $(this).removeClass("invalid");
    }

  });

  if (firstInvalidRowIndex > 0) {
    $('html, body').animate({
      scrollTop: ($("#alertr1").offset().top)
    }, 1000);
  }
}

function valueE(e) {
  var newValue = e.target.value;
  var $radio = $(e.target);

  if ($radio.is('.Fr')) {
    $radio.closest('tr').find('.numFr').val(newValue);
  } else {
    $radio.closest('tr').find('.numIr').val(newValue);
  }
}

$(document).ready(function() {
  importResultJson();

});

function exportResultJson() {
  var fav, favs = [];
  $('.allrb').each(function() { // run through each of the checkboxes
    fav = {
      name: $(this).attr('name'),
      value: $(this).prop('checked')
    };
    favs.push(fav);
  });

  localStorage.setItem("favorites", JSON.stringify(favs));
  console.log(favs);
}

function importResultJson() {
  var favorites = JSON.parse(localStorage.getItem('favorites'));
  if (!favorites.length) {
    return
  };
  console.debug(favorites);

  for (var i = 0; i < favorites.length; i++) {
    //console.debug(favorites[i].value == 'on');
    //$('#' + favorites[i].id ).prop('checked', favorites[i].value);
    $('[name=' + favorites[i].name + ']').prop('checked', favorites[i].value);

  }

}

function page_onLoad() {
  if (Moodle_getParam("attempt_id")) {
    Moodle_Retrieve_QuizResult(Moodle_getParam("attempt_id"), function(data) {
      importResultJson();
    });
  }

}```
<pre class="snippet-code-css lang-css prettyprint-override"><code>input[type=number] {
  width: 25px;
  text-align: center;
}

.invalid {
  outline: 1px solid red;
}</code>```

    content 1

        <input type="radio" name="Fr1" class="allrb Fr" value="0">

        <input type="radio" name="Fr1" class="allrb Fr" value="1">

        <input type="radio" name="Fr1" class="allrb Fr" value="2">

        <input type="radio" name="Fr1" class="allrb Fr" value="3">

        <input type="radio" name="Fr1" class="allrb Fr" value="4">

        <input type="radio" name="Ir1" class="allrb Ir" value="0">

        <input type="radio" name="Ir1" class="allrb Ir" value="1">

        <input type="radio" name="Ir1" class="allrb Ir" value="2">

        <input type="radio" name="Ir1" class="allrb Ir" value="3">

        <input type="radio" name="Ir1" class="allrb Ir" value="4">

      <input type="number" name="numFr1" class="numFr" id="numFr1" disabled>

      <input type="number" name="numIr1" class="numIr" id="numIr1" disabled>

    content 2

        <input type="radio" name="Fr2" class="allrb Fr" value="0">

        <input type="radio" name="Fr2" class="allrb Fr" value="1">

        <input type="radio" name="Fr2" class="allrb Fr" value="2">

        <input type="radio" name="Fr2" class="allrb Fr" value="3">

        <input type="radio" name="Fr2" class="allrb Fr" value="4">

        <input type="radio" name="Ir2" class="allrb Ir" value="0">

        <input type="radio" name="Ir2" class="allrb Ir" value="1">

        <input type="radio" name="Ir2" class="allrb Ir" value="2">

        <input type="radio" name="Ir2" class="allrb Ir" value="3">

        <input type="radio" name="Ir2" class="allrb Ir" value="4">

      <input type="number" name="numFr2" class="numFr" id="numFr2" disabled>

      <input type="number" name="numIr2" class="numIr" id="numIr2" disabled>

    content 3

        <input type="radio" name="Fr3" class="allrb Fr" value="0">

        <input type="radio" name="Fr3" class="allrb Fr" value="1">

        <input type="radio" name="Fr3" class="allrb Fr" value="2">

        <input type="radio" name="Fr3" class="allrb Fr" value="3">

        <input type="radio" name="Fr3" class="allrb Fr" value="4">

        <input type="radio" name="Ir3" class="allrb" value="0">

        <input type="radio" name="Ir3" class="allrb" value="1">

        <input type="radio" name="Ir3" class="allrb" value="2">

        <input type="radio" name="Ir3" class="allrb" value="3">

        <input type="radio" name="Ir3" class="allrb" value="4">

      <input type="number" name="numFr3" class="numFr" id="numFr3" disabled>

      <input type="number" name="numIr3" class="numIr" id="numIr3" disabled>

    content 4

        <input type="radio" name="Fr4" class="allrb Fr" value="0">

        <input type="radio" name="Fr4" class="allrb Fr" value="1">

        <input type="radio" name="Fr4" class="allrb Fr" value="2">

        <input type="radio" name="Fr4" class="allrb Fr" value="3">

        <input type="radio" name="Fr4" class="allrb Fr" value="4">

        <input type="radio" name="Ir4" class="allrb" value="0">

        <input type="radio" name="Ir4" class="allrb" value="1">

        <input type="radio" name="Ir4" class="allrb" value="2">

        <input type="radio" name="Ir4" class="allrb" value="3">

        <input type="radio" name="Ir4" class="allrb" value="4">

      <input type="number" name="numFr4" class="numFr" id="numFr4" disabled>

      <input type="number" name="numIr4" class="numIr" id="numIr4" disabled>

    content 5

        <input type="radio" name="Fr5" class="allrb Fr" value="0">

        <input type="radio" name="Fr5" class="allrb Fr" value="1">

        <input type="radio" name="Fr5" class="allrb Fr" value="2">

        <input type="radio" name="Fr5" class="allrb Fr" value="3">

        <input type="radio" name="Fr5" class="allrb Fr" value="4">

        <input type="radio" name="Ir5" class="allrb" value="0">

        <input type="radio" name="Ir5" class="allrb" value="1">

        <input type="radio" name="Ir5" class="allrb" value="2">

        <input type="radio" name="Ir5" class="allrb" value="3">

        <input type="radio" name="Ir5" class="allrb" value="4">

      <input type="number" name="numFr5" class="numFr" id="numFr5" disabled>

      <input type="number" name="numIr5" class="numIr" id="numIr5" disabled>

    content 6

        <input type="radio" name="Fr6" class="allrb Fr" value="0">

        <input type="radio" name="Fr6" class="allrb Fr" value="1">

        <input type="radio" name="Fr6" class="allrb Fr" value="2">

        <input type="radio" name="Fr6" class="allrb Fr" value="3">

        <input type="radio" name="Fr6" class="allrb Fr" value="4">

        <input type="radio" name="Ir6" class="allrb" value="0">

        <input type="radio" name="Ir6" class="allrb" value="1">

        <input type="radio" name="Ir6" class="allrb" value="2">

        <input type="radio" name="Ir6" class="allrb" value="3">

        <input type="radio" name="Ir6" class="allrb" value="4">

      <input type="number" name="numFr6" class="numFr" id="numFr6" disabled>

      <input type="number" name="numIr6" class="numIr" id="numIr6" disabled>

    content 7

        <input type="radio" name="Fr7" class="allrb Fr" value="0">

        <input type="radio" name="Fr7" class="allrb Fr" value="1">

        <input type="radio" name="Fr7" class="allrb Fr" value="2">

        <input type="radio" name="Fr7" class="allrb Fr" value="3">

        <input type="radio" name="Fr7" class="allrb Fr" value="4">

        <input type="radio" name="Ir7" class="allrb" value="0">

        <input type="radio" name="Ir7" class="allrb" value="1">

        <input type="radio" name="Ir7" class="allrb" value="2">

        <input type="radio" name="Ir7" class="allrb" value="3">

        <input type="radio" name="Ir7" class="allrb" value="4">

      <input type="number" name="numFr7" class="numFr" id="numFr7" disabled>

      <input type="number" name="numIr7" class="numIr" id="numIr7" disabled>

    content 8

        <input type="radio" name="Fr8" class="allrb Fr" value="0">

        <input type="radio" name="Fr8" class="allrb Fr" value="1">

        <input type="radio" name="Fr8" class="allrb Fr" value="2">

        <input type="radio" name="Fr8" class="allrb Fr" value="3">

        <input type="radio" name="Fr8" class="allrb Fr" value="4">

        <input type="radio" name="Ir8" class="allrb" value="0">

        <input type="radio" name="Ir8" class="allrb" value="1">

        <input type="radio" name="Ir8" class="allrb" value="2">

        <input type="radio" name="Ir8" class="allrb" value="3">

        <input type="radio" name="Ir8" class="allrb" value="4">

      <input type="number" name="numFr8" class="numFr" id="numFr8" disabled>

      <input type="number" name="numIr8" class="numIr" id="numIr8" disabled>

    content 9

        <input type="radio" name="Fr9" class="allrb Fr" value="0">

        <input type="radio" name="Fr9" class="allrb Fr" value="1">

        <input type="radio" name="Fr9" class="allrb Fr" value="2">

        <input type="radio" name="Fr9" class="allrb Fr" value="3">

        <input type="radio" name="Fr9" class="allrb Fr" value="4">

        <input type="radio" name="Ir9" class="allrb" value="0">

        <input type="radio" name="Ir9" class="allrb" value="1">

        <input type="radio" name="Ir9" class="allrb" value="2">

        <input type="radio" name="Ir9" class="allrb" value="3">

        <input type="radio" name="Ir9" class="allrb" value="4">

      <input type="number" name="numFr9" class="numFr" id="numFr9" disabled>

      <input type="number" name="numIr9" class="numIr" id="numIr9" disabled>

<button class="btn btn-primary" id="showme" type="button">View Result</button>

```

I had already success to store all the radio button value into Json using localstorage setItem, but I don't know why, it call only half of my store value. You may understand when you see my fiddle.
First you had to select some value for radio box then click the button and refresh again the page.

After refresh. May I know where the probelm is? Thanks.

Here is the fiddle.

答:
The problem is that you're just putting the name of the radio button into the local storage elements. But all the buttons in a group have the same name, and you don't distinguish which button in the group you're storing.

You should just store the value of the buttons that are checked. Then when restoring, you check that specific button in each group.

$(document).ready(function() {

  $('#showme').click(function() {
    //validateNresult() ;
    exportResultJson();
  })

  $('.allrb').change(valueE);

});

$(document).ready(function() {
  importResultJson();

});

function exportResultJson() {
  var fav, favs = [];
  $('.allrb:checked').each(function() { // run through each of the checkboxes
    fav = {
      name: $(this).attr('name'),
      value: $(this).val()
    };
    favs.push(fav);
  });

  localStorage.setItem("favorites", JSON.stringify(favs));
  console.log(favs);
}

function importResultJson() {
  var favorites = JSON.parse(localStorage.getItem('favorites'));
  if (!favorites.length) {
    return
  };
  console.debug(favorites);

  for (var i = 0; i < favorites.length; i++) {
    //console.debug(favorites[i].value == 'on');
    //$('#' + favorites[i].id ).prop('checked', favorites[i].value);
    $('[name=' + favorites[i].name + '][value=' + favorites[i].value + ']').prop('checked', true);

  }

}

function valueE(e) {
  var newValue = e.target.value;
  var $radio = $(e.target);

  if ($radio.is('.Fr')) {
    $radio.closest('tr').find('.numFr').val(newValue);
  } else {
    $radio.closest('tr').find('.numIr').val(newValue);
  }
}```
<pre class="snippet-code-css lang-css prettyprint-override"><code>input[type=number] {
  width: 25px;
  text-align: center;
}

.invalid {
  outline: 1px solid red;
}</code>```

    content 1

        <input type="radio" name="Fr1" class="allrb Fr" value="0">

        <input type="radio" name="Fr1" class="allrb Fr" value="1">

        <input type="radio" name="Fr1" class="allrb Fr" value="2">

        <input type="radio" name="Fr1" class="allrb Fr" value="3">

        <input type="radio" name="Fr1" class="allrb Fr" value="4">

        <input type="radio" name="Ir1" class="allrb Ir" value="0">

        <input type="radio" name="Ir1" class="allrb Ir" value="1">

        <input type="radio" name="Ir1" class="allrb Ir" value="2">

        <input type="radio" name="Ir1" class="allrb Ir" value="3">

        <input type="radio" name="Ir1" class="allrb Ir" value="4">

      <input type="number" name="numFr1" class="numFr" id="numFr1" disabled>

      <input type="number" name="numIr1" class="numIr" id="numIr1" disabled>

    content 2

        <input type="radio" name="Fr2" class="allrb Fr" value="0">

        <input type="radio" name="Fr2" class="allrb Fr" value="1">

        <input type="radio" name="Fr2" class="allrb Fr" value="2">

        <input type="radio" name="Fr2" class="allrb Fr" value="3">

        <input type="radio" name="Fr2" class="allrb Fr" value="4">

        <input type="radio" name="Ir2" class="allrb Ir" value="0">

        <input type="radio" name="Ir2" class="allrb Ir" value="1">

        <input type="radio" name="Ir2" class="allrb Ir" value="2">

        <input type="radio" name="Ir2" class="allrb Ir" value="3">

        <input type="radio" name="Ir2" class="allrb Ir" value="4">

      <input type="number" name="numFr2" class="numFr" id="numFr2" disabled>

      <input type="number" name="numIr2" class="numIr" id="numIr2" disabled>

    content 3

        <input type="radio" name="Fr3" class="allrb Fr" value="0">

        <input type="radio" name="Fr3" class="allrb Fr" value="1">

        <input type="radio" name="Fr3" class="allrb Fr" value="2">

        <input type="radio" name="Fr3" class="allrb Fr" value="3">

        <input type="radio" name="Fr3" class="allrb Fr" value="4">

        <input type="radio" name="Ir3" class="allrb" value="0">

        <input type="radio" name="Ir3" class="allrb" value="1">

        <input type="radio" name="Ir3" class="allrb" value="2">

        <input type="radio" name="Ir3" class="allrb" value="3">

        <input type="radio" name="Ir3" class="allrb" value="4">

      <input type="number" name="numFr3" class="numFr" id="numFr3" disabled>

      <input type="number" name="numIr3" class="numIr" id="numIr3" disabled>

    content 4

        <input type="radio" name="Fr4" class="allrb Fr" value="0">

        <input type="radio" name="Fr4" class="allrb Fr" value="1">

        <input type="radio" name="Fr4" class="allrb Fr" value="2">

        <input type="radio" name="Fr4" class="allrb Fr" value="3">

        <input type="radio" name="Fr4" class="allrb Fr" value="4">

        <input type="radio" name="Ir4" class="allrb" value="0">

        <input type="radio" name="Ir4" class="allrb" value="1">

        <input type="radio" name="Ir4" class="allrb" value="2">

        <input type="radio" name="Ir4" class="allrb" value="3">

        <input type="radio" name="Ir4" class="allrb" value="4">

      <input type="number" name="numFr4" class="numFr" id="numFr4" disabled>

      <input type="number" name="numIr4" class="numIr" id="numIr4" disabled>

    content 5

        <input type="radio" name="Fr5" class="allrb Fr" value="0">

        <input type="radio" name="Fr5" class="allrb Fr" value="1">

        <input type="radio" name="Fr5" class="allrb Fr" value="2">

        <input type="radio" name="Fr5" class="allrb Fr" value="3">

        <input type="radio" name="Fr5" class="allrb Fr" value="4">

        <input type="radio" name="Ir5" class="allrb" value="0">

        <input type="radio" name="Ir5" class="allrb" value="1">

        <input type="radio" name="Ir5" class="allrb" value="2">

        <input type="radio" name="Ir5" class="allrb" value="3">

        <input type="radio" name="Ir5" class="allrb" value="4">

      <input type="number" name="numFr5" class="numFr" id="numFr5" disabled>

      <input type="number" name="numIr5" class="numIr" id="numIr5" disabled>

    content 6

        <input type="radio" name="Fr6" class="allrb Fr" value="0">

        <input type="radio" name="Fr6" class="allrb Fr" value="1">

        <input type="radio" name="Fr6" class="allrb Fr" value="2">

        <input type="radio" name="Fr6" class="allrb Fr" value="3">

        <input type="radio" name="Fr6" class="allrb Fr" value="4">

        <input type="radio" name="Ir6" class="allrb" value="0">

        <input type="radio" name="Ir6" class="allrb" value="1">

        <input type="radio" name="Ir6" class="allrb" value="2">

        <input type="radio" name="Ir6" class="allrb" value="3">

        <input type="radio" name="Ir6" class="allrb" value="4">

      <input type="number" name="numFr6" class="numFr" id="numFr6" disabled>

      <input type="number" name="numIr6" class="numIr" id="numIr6" disabled>

    content 7

        <input type="radio" name="Fr7" class="allrb Fr" value="0">

        <input type="radio" name="Fr7" class="allrb Fr" value="1">

        <input type="radio" name="Fr7" class="allrb Fr" value="2">

        <input type="radio" name="Fr7" class="allrb Fr" value="3">

        <input type="radio" name="Fr7" class="allrb Fr" value="4">

        <input type="radio" name="Ir7" class="allrb" value="0">

        <input type="radio" name="Ir7" class="allrb" value="1">

        <input type="radio" name="Ir7" class="allrb" value="2">

        <input type="radio" name="Ir7" class="allrb" value="3">

        <input type="radio" name="Ir7" class="allrb" value="4">

      <input type="number" name="numFr7" class="numFr" id="numFr7" disabled>

      <input type="number" name="numIr7" class="numIr" id="numIr7" disabled>

    content 8

        <input type="radio" name="Fr8" class="allrb Fr" value="0">

        <input type="radio" name="Fr8" class="allrb Fr" value="1">

        <input type="radio" name="Fr8" class="allrb Fr" value="2">

        <input type="radio" name="Fr8" class="allrb Fr" value="3">

        <input type="radio" name="Fr8" class="allrb Fr" value="4">

        <input type="radio" name="Ir8" class="allrb" value="0">

        <input type="radio" name="Ir8" class="allrb" value="1">

        <input type="radio" name="Ir8" class="allrb" value="2">

        <input type="radio" name="Ir8" class="allrb" value="3">

        <input type="radio" name="Ir8" class="allrb" value="4">

      <input type="number" name="numFr8" class="numFr" id="numFr8" disabled>

      <input type="number" name="numIr8" class="numIr" id="numIr8" disabled>

    content 9

        <input type="radio" name="Fr9" class="allrb Fr" value="0">

        <input type="radio" name="Fr9" class="allrb Fr" value="1">

        <input type="radio" name="Fr9" class="allrb Fr" value="2">

        <input type="radio" name="Fr9" class="allrb Fr" value="3">

        <input type="radio" name="Fr9" class="allrb Fr" value="4">

        <input type="radio" name="Ir9" class="allrb" value="0">

        <input type="radio" name="Ir9" class="allrb" value="1">

        <input type="radio" name="Ir9" class="allrb" value="2">

        <input type="radio" name="Ir9" class="allrb" value="3">

        <input type="radio" name="Ir9" class="allrb" value="4">

      <input type="number" name="numFr9" class="numFr" id="numFr9" disabled>

      <input type="number" name="numIr9" class="numIr" id="numIr9" disabled>

<button class="btn btn-primary" id="showme" type="button">View Result</button>

```

jsfiddle demo

Body:
$(document).ready(function() {

  $('#showme').click(function() {
    //validateNresult() ;
    exportResultJson();
  })

  $('.allrb').change(valueE);

});

function validateNresult() {

  var firstInvalidRowIndex = 0;
  $.each($("tbody tr"), function(i, e) {
    var hasInvalidInputs = $(this).find(".numFr, .numIr").filter(function() {
      return $.trim(this.value).length === 0;
    }).length > 0;

    if (hasInvalidInputs) {
      $(this).addClass("invalid");
      if (firstInvalidRowIndex <= 0) {
        firstInvalidRowIndex = i + 1;
      }
    } else {
      $(this).removeClass("invalid");
    }

  });

  if (firstInvalidRowIndex > 0) {
    $('html, body').animate({
      scrollTop: ($("#alertr1").offset().top)
    }, 1000);
  }
}

function valueE(e) {
  var newValue = e.target.value;
  var $radio = $(e.target);

  if ($radio.is('.Fr')) {
    $radio.closest('tr').find('.numFr').val(newValue);
  } else {
    $radio.closest('tr').find('.numIr').val(newValue);
  }
}

$(document).ready(function() {
  importResultJson();

});

function exportResultJson() {
  var fav, favs = [];
  $('.allrb').each(function() { // run through each of the checkboxes
    fav = {
      name: $(this).attr('name'),
      value: $(this).prop('checked')
    };
    favs.push(fav);
  });

  localStorage.setItem("favorites", JSON.stringify(favs));
  console.log(favs);
}

function importResultJson() {
  var favorites = JSON.parse(localStorage.getItem('favorites'));
  if (!favorites.length) {
    return
  };
  console.debug(favorites);

  for (var i = 0; i < favorites.length; i++) {
    //console.debug(favorites[i].value == 'on');
    //$('#' + favorites[i].id ).prop('checked', favorites[i].value);
    $('[name=' + favorites[i].name + ']').prop('checked', favorites[i].value);

  }

}

function page_onLoad() {
  if (Moodle_getParam("attempt_id")) {
    Moodle_Retrieve_QuizResult(Moodle_getParam("attempt_id"), function(data) {
      importResultJson();
    });
  }

}

input[type=number] {
  width: 25px;
  text-align: center;
}

.invalid {
  outline: 1px solid red;
}

    content 1

        <input type="radio" name="Fr1" class="allrb Fr" value="0">

        <input type="radio" name="Fr1" class="allrb Fr" value="1">

        <input type="radio" name="Fr1" class="allrb Fr" value="2">

        <input type="radio" name="Fr1" class="allrb Fr" value="3">

        <input type="radio" name="Fr1" class="allrb Fr" value="4">

        <input type="radio" name="Ir1" class="allrb Ir" value="0">

        <input type="radio" name="Ir1" class="allrb Ir" value="1">

        <input type="radio" name="Ir1" class="allrb Ir" value="2">

        <input type="radio" name="Ir1" class="allrb Ir" value="3">

        <input type="radio" name="Ir1" class="allrb Ir" value="4">

      <input type="number" name="numFr1" class="numFr" id="numFr1" disabled>

      <input type="number" name="numIr1" class="numIr" id="numIr1" disabled>

    content 2

        <input type="radio" name="Fr2" class="allrb Fr" value="0">

        <input type="radio" name="Fr2" class="allrb Fr" value="1">

        <input type="radio" name="Fr2" class="allrb Fr" value="2">

        <input type="radio" name="Fr2" class="allrb Fr" value="3">

        <input type="radio" name="Fr2" class="allrb Fr" value="4">

        <input type="radio" name="Ir2" class="allrb Ir" value="0">

        <input type="radio" name="Ir2" class="allrb Ir" value="1">

        <input type="radio" name="Ir2" class="allrb Ir" value="2">

        <input type="radio" name="Ir2" class="allrb Ir" value="3">

        <input type="radio" name="Ir2" class="allrb Ir" value="4">

      <input type="number" name="numFr2" class="numFr" id="numFr2" disabled>

      <input type="number" name="numIr2" class="numIr" id="numIr2" disabled>

    content 3

        <input type="radio" name="Fr3" class="allrb Fr" value="0">

        <input type="radio" name="Fr3" class="allrb Fr" value="1">

        <input type="radio" name="Fr3" class="allrb Fr" value="2">

        <input type="radio" name="Fr3" class="allrb Fr" value="3">

        <input type="radio" name="Fr3" class="allrb Fr" value="4">

        <input type="radio" name="Ir3" class="allrb" value="0">

        <input type="radio" name="Ir3" class="allrb" value="1">

        <input type="radio" name="Ir3" class="allrb" value="2">

        <input type="radio" name="Ir3" class="allrb" value="3">

        <input type="radio" name="Ir3" class="allrb" value="4">

      <input type="number" name="numFr3" class="numFr" id="numFr3" disabled>

      <input type="number" name="numIr3" class="numIr" id="numIr3" disabled>

    content 4

        <input type="radio" name="Fr4" class="allrb Fr" value="0">

        <input type="radio" name="Fr4" class="allrb Fr" value="1">

        <input type="radio" name="Fr4" class="allrb Fr" value="2">

        <input type="radio" name="Fr4" class="allrb Fr" value="3">

        <input type="radio" name="Fr4" class="allrb Fr" value="4">

        <input type="radio" name="Ir4" class="allrb" value="0">

        <input type="radio" name="Ir4" class="allrb" value="1">

        <input type="radio" name="Ir4" class="allrb" value="2">

        <input type="radio" name="Ir4" class="allrb" value="3">

        <input type="radio" name="Ir4" class="allrb" value="4">

      <input type="number" name="numFr4" class="numFr" id="numFr4" disabled>

      <input type="number" name="numIr4" class="numIr" id="numIr4" disabled>

    content 5

        <input type="radio" name="Fr5" class="allrb Fr" value="0">

        <input type="radio" name="Fr5" class="allrb Fr" value="1">

        <input type="radio" name="Fr5" class="allrb Fr" value="2">

        <input type="radio" name="Fr5" class="allrb Fr" value="3">

        <input type="radio" name="Fr5" class="allrb Fr" value="4">

        <input type="radio" name="Ir5" class="allrb" value="0">

        <input type="radio" name="Ir5" class="allrb" value="1">

        <input type="radio" name="Ir5" class="allrb" value="2">

        <input type="radio" name="Ir5" class="allrb" value="3">

        <input type="radio" name="Ir5" class="allrb" value="4">

      <input type="number" name="numFr5" class="numFr" id="numFr5" disabled>

      <input type="number" name="numIr5" class="numIr" id="numIr5" disabled>

    content 6

        <input type="radio" name="Fr6" class="allrb Fr" value="0">

        <input type="radio" name="Fr6" class="allrb Fr" value="1">

        <input type="radio" name="Fr6" class="allrb Fr" value="2">

        <input type="radio" name="Fr6" class="allrb Fr" value="3">

        <input type="radio" name="Fr6" class="allrb Fr" value="4">

        <input type="radio" name="Ir6" class="allrb" value="0">

        <input type="radio" name="Ir6" class="allrb" value="1">

        <input type="radio" name="Ir6" class="allrb" value="2">

        <input type="radio" name="Ir6" class="allrb" value="3">

        <input type="radio" name="Ir6" class="allrb" value="4">

      <input type="number" name="numFr6" class="numFr" id="numFr6" disabled>

      <input type="number" name="numIr6" class="numIr" id="numIr6" disabled>

    content 7

        <input type="radio" name="Fr7" class="allrb Fr" value="0">

        <input type="radio" name="Fr7" class="allrb Fr" value="1">

        <input type="radio" name="Fr7" class="allrb Fr" value="2">

        <input type="radio" name="Fr7" class="allrb Fr" value="3">

        <input type="radio" name="Fr7" class="allrb Fr" value="4">

        <input type="radio" name="Ir7" class="allrb" value="0">

        <input type="radio" name="Ir7" class="allrb" value="1">

        <input type="radio" name="Ir7" class="allrb" value="2">

        <input type="radio" name="Ir7" class="allrb" value="3">

        <input type="radio" name="Ir7" class="allrb" value="4">

      <input type="number" name="numFr7" class="numFr" id="numFr7" disabled>

      <input type="number" name="numIr7" class="numIr" id="numIr7" disabled>

    content 8

        <input type="radio" name="Fr8" class="allrb Fr" value="0">

        <input type="radio" name="Fr8" class="allrb Fr" value="1">

        <input type="radio" name="Fr8" class="allrb Fr" value="2">

        <input type="radio" name="Fr8" class="allrb Fr" value="3">

        <input type="radio" name="Fr8" class="allrb Fr" value="4">

        <input type="radio" name="Ir8" class="allrb" value="0">

        <input type="radio" name="Ir8" class="allrb" value="1">

        <input type="radio" name="Ir8" class="allrb" value="2">

        <input type="radio" name="Ir8" class="allrb" value="3">

        <input type="radio" name="Ir8" class="allrb" value="4">

      <input type="number" name="numFr8" class="numFr" id="numFr8" disabled>

      <input type="number" name="numIr8" class="numIr" id="numIr8" disabled>

    content 9

        <input type="radio" name="Fr9" class="allrb Fr" value="0">

        <input type="radio" name="Fr9" class="allrb Fr" value="1">

        <input type="radio" name="Fr9" class="allrb Fr" value="2">

        <input type="radio" name="Fr9" class="allrb Fr" value="3">

        <input type="radio" name="Fr9" class="allrb Fr" value="4">

        <input type="radio" name="Ir9" class="allrb" value="0">

        <input type="radio" name="Ir9" class="allrb" value="1">

        <input type="radio" name="Ir9" class="allrb" value="2">

        <input type="radio" name="Ir9" class="allrb" value="3">

        <input type="radio" name="Ir9" class="allrb" value="4">

      <input type="number" name="numFr9" class="numFr" id="numFr9" disabled>

      <input type="number" name="numIr9" class="numIr" id="numIr9" disabled>

<button class="btn btn-primary" id="showme" type="button">View Result</button>

I had already success to store all the radio button value into Json using localstorage setItem, but I don't know why, it call only half of my store value. You may understand when you see my fiddle.
First you had to select some value for radio box then click the button and refresh again the page.

After refresh. May I know where the probelm is? Thanks.

Here is the fiddle.

Body:
The problem is that you're just putting the name of the radio button into the local storage elements. But all the buttons in a group have the same name, and you don't distinguish which button in the group you're storing.

You should just store the value of the buttons that are checked. Then when restoring, you check that specific button in each group.

$(document).ready(function() {

  $('#showme').click(function() {
    //validateNresult() ;
    exportResultJson();
  })

  $('.allrb').change(valueE);

});

$(document).ready(function() {
  importResultJson();

});

function exportResultJson() {
  var fav, favs = [];
  $('.allrb:checked').each(function() { // run through each of the checkboxes
    fav = {
      name: $(this).attr('name'),
      value: $(this).val()
    };
    favs.push(fav);
  });

  localStorage.setItem("favorites", JSON.stringify(favs));
  console.log(favs);
}

function importResultJson() {
  var favorites = JSON.parse(localStorage.getItem('favorites'));
  if (!favorites.length) {
    return
  };
  console.debug(favorites);

  for (var i = 0; i < favorites.length; i++) {
    //console.debug(favorites[i].value == 'on');
    //$('#' + favorites[i].id ).prop('checked', favorites[i].value);
    $('[name=' + favorites[i].name + '][value=' + favorites[i].value + ']').prop('checked', true);

  }

}

function valueE(e) {
  var newValue = e.target.value;
  var $radio = $(e.target);

  if ($radio.is('.Fr')) {
    $radio.closest('tr').find('.numFr').val(newValue);
  } else {
    $radio.closest('tr').find('.numIr').val(newValue);
  }
}

input[type=number] {
  width: 25px;
  text-align: center;
}

.invalid {
  outline: 1px solid red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-hover table-responsive" id="tab">
  <tr id="alertr1" name="v">
    <td width="200px" id="text">content 1</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr1" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr1" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr1" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr1" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr1" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir1" class="allrb Ir" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir1" class="allrb Ir" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir1" class="allrb Ir" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir1" class="allrb Ir" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir1" class="allrb Ir" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr1" class="numFr" id="numFr1" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr1" class="numIr" id="numIr1" disabled>
    </td>
  </tr>

  <tr id="alertr2" name="v">
    <td width="200px" id="text">content 2</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr2" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr2" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr2" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr2" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr2" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir2" class="allrb Ir" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir2" class="allrb Ir" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir2" class="allrb Ir" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir2" class="allrb Ir" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir2" class="allrb Ir" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr2" class="numFr" id="numFr2" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr2" class="numIr" id="numIr2" disabled>
    </td>
  </tr>

  <tr id="alertr3" name="v">
    <td width="200px" id="text">content 3</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr3" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr3" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr3" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr3" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr3" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir3" class="allrb" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir3" class="allrb" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir3" class="allrb" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir3" class="allrb" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir3" class="allrb" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr3" class="numFr" id="numFr3" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr3" class="numIr" id="numIr3" disabled>
    </td>
  </tr>

  <tr id="alertr4" name="v">
    <td width="200px" id="text">content 4</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr4" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr4" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr4" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr4" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr4" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir4" class="allrb" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir4" class="allrb" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir4" class="allrb" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir4" class="allrb" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir4" class="allrb" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr4" class="numFr" id="numFr4" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr4" class="numIr" id="numIr4" disabled>
    </td>
  </tr>

  <tr id="alertr5" name="v">
    <td width="200px" id="text">content 5</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr5" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr5" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr5" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr5" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr5" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir5" class="allrb" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir5" class="allrb" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir5" class="allrb" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir5" class="allrb" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir5" class="allrb" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr5" class="numFr" id="numFr5" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr5" class="numIr" id="numIr5" disabled>
    </td>
  </tr>
  <tr id="alertr6" name="v">
    <td width="200px" id="text">content 6</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr6" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr6" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr6" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr6" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr6" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir6" class="allrb" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir6" class="allrb" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir6" class="allrb" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir6" class="allrb" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir6" class="allrb" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr6" class="numFr" id="numFr6" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr6" class="numIr" id="numIr6" disabled>
    </td>
  </tr>
  <tr id="alertr7" name="v">
    <td width="200px" id="text">content 7</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr7" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr7" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr7" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr7" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr7" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir7" class="allrb" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir7" class="allrb" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir7" class="allrb" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir7" class="allrb" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir7" class="allrb" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr7" class="numFr" id="numFr7" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr7" class="numIr" id="numIr7" disabled>
    </td>
  </tr>
  <tr id="alertr8" name="v">
    <td width="200px" id="text">content 8</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr8" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr8" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr8" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr8" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr8" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir8" class="allrb" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir8" class="allrb" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir8" class="allrb" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir8" class="allrb" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir8" class="allrb" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr8" class="numFr" id="numFr8" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr8" class="numIr" id="numIr8" disabled>
    </td>
  </tr>
  <tr id="alertr9" name="v">
    <td width="200px" id="text">content 9</td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr9" class="allrb Fr" value="0">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr9" class="allrb Fr" value="1">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr9" class="allrb Fr" value="2">
      </label>
    </td>
    <td width="40px">
      <label>
        <input type="radio" name="Fr9" class="allrb Fr" value="3">
      </label>
    </td>
    <td width="30px">
      <label>
        <input type="radio" name="Fr9" class="allrb Fr" value="4">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir9" class="allrb" value="0">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir9" class="allrb" value="1">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir9" class="allrb" value="2">
      </label>
    </td>
    <td width="50px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir9" class="allrb" value="3">
      </label>
    </td>
    <td width="120px" bgcolor="Gainsboro">
      <label>
        <input type="radio" name="Ir9" class="allrb" value="4">
      </label>
    </td>
    <td width="30px">
      <input type="number" name="numFr9" class="numFr" id="numFr9" disabled>
    </td>
    <td width="30px" bgcolor="Gainsboro">
      <input type="number" name="numIr9" class="numIr" id="numIr9" disabled>
    </td>
  </tr>

</table>

<button class="btn btn-primary" id="showme" type="button">View Result</button>
<p style="float:right;position:absolute;margin-left:700px;margin-top:10px;" id="alertmessage"></p>

jsfiddle demo


来源: stackexchange

问:
SQL Databinding to Dropdown Lists in Repeaters C#
Ok guys, so I am trying to bind data into a dropdown list from c#. I am getting a Null error when trying to enter the data into the DDL's.  I am using this code for the front end.

```<code> <asp:Repeater ID="RepeaterHardDrives" runat="server">
                    <HeaderTemplate>
                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:HiddenField ID="hidHardDrivesPackageDefaultID" runat="server" />
                                <asp:HiddenField ID="hidHardDrivesPackageDefaultPrice" runat="server" />
                                <span>
                                    <asp:Label runat="server" ID="lbHardDiskPrice" Text="$00.00/mo"></asp:Label></span><label>Hard
                                        Drive:</label><asp:DropDownList ID="ddHardDrive" DataTextField="ItemName" DataValueField="ProductItemID" runat="server" CssClass="lidropdown">
                                        </asp:DropDownList>
                                <asp:ImageButton runat="server" ID="ShowHarddriveInfo" ImageUrl="/_Images/server_configurator_helpbutton.png"
                                    OnClick="lnkShowHarddriveInfo_OnClick" /></div>
                            </td>
                            <td align="right">
                                <asp:Label runat="server" ID="lbHardDrivesPrice" />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                        <br />
                    </FooterTemplate>
                </asp:Repeater>
</code>```

for the backend I am trying to load a dynamic number of Dropdown lists into the repeater then databind them all with the same data.

```<code>  public void PopulateHardDrives(int intSupportedDrives)
  {
    PreloadHardDriveRepeater(intSupportedDrives);

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["connstrname"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Prod_SelectIDNamePriceByCategory", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@CategoryCode", "Hard Drive");
    DataTable dtHardDrives = new DataTable();
    using (conn)
    {
      conn.Open();
      SqlDataReader dr = cmd.ExecuteReader();
      dtHardDrives.Load(dr);
      ViewState.Add("dtHardDrives", dtHardDrives);
    }

    foreach (RepeaterItem riHardDrive in RepeaterHardDrives.Items)
    {
      DropDownList ddHardDrives = (DropDownList)riHardDrive.FindControl("ddHardDrives");
      ddHardDrives.DataSource = dtHardDrives;//program gives NULL exception error here(object not set to instance of object however it know the count of the rows it is supposed to be pulling)
      ddHardDrives.DataValueField = "ProductItemID";
      ddHardDrives.DataTextField = "ItemName";
      ddHardDrives.DataBind();
      Label lbHardDrive = (Label)riHardDrive.FindControl("lbHardDrivesPrice");
      lbHardDrive.Text = String.Format("{0:c}", Convert.ToDecimal("0.00"));
      if (riHardDrive.ItemIndex != 0) //We do not want to allow None to be selected on the main drive
      {
        ddHardDrives.Items.Insert(0, "None");
      }
    }
  }

and last but not least the function to setup the dynamic amount of DDL's looks like this

     private void PreloadHardDriveRepeater(int intSupportedDrives)
    {
        int[] intArrDisks = new int[intSupportedDrives];
        for (int intDiskCount = 0; intDiskCount < intArrDisks.Length; intDiskCount++)
        {
            intArrDisks[intDiskCount] = intDiskCount;
        }
        RepeaterHardDrives.DataSource = intArrDisks;
        RepeaterHardDrives.DataBind();
    }
</code>```

I am calling a list of populate functions in a !page.isPostBack if statement and the only one that is not getting the data is this one with the Drown Lists.  It gets the number of Rows(18) from the database, but it it throwing a Null error(Object reference not set to an instance of an object.) I have seen quite a few people have been running into this error while googling the problem, however I could not find a solution that worked for me. The PreloadHardDriveRepeater function seems to work fine when run alone it loads the correct amount of DDL's onto the page.

Thanks ahead of time.

答:
Your control is "ddHardDrive":

```<code><asp:DropDownList ID="ddHardDrive" DataTextField="ItemName" DataValueField="ProductItemID" runat="server" CssClass="lidropdown">
</code>```

and your code is looking for "ddHardDrives"

```<code>riHardDrive.FindControl("ddHardDrives");
</code>```

This would be easy to notice if you debugged into the function and looked at your variable values right before the exception is thrown.

ContentLicense: CC BY-SA 2.5

Body:
Ok guys, so I am trying to bind data into a dropdown list from c#. I am getting a Null error when trying to enter the data into the DDL's.  I am using this code for the front end.

 <asp:Repeater ID="RepeaterHardDrives" runat="server">
                    <HeaderTemplate>

                    </HeaderTemplate>
                    <ItemTemplate>

                                <asp:HiddenField ID="hidHardDrivesPackageDefaultID" runat="server" />
                                <asp:HiddenField ID="hidHardDrivesPackageDefaultPrice" runat="server" />

                                    <asp:Label runat="server" ID="lbHardDiskPrice" Text="$00.00/mo"></asp:Label>Hard
                                        Drive:<asp:DropDownList ID="ddHardDrive" DataTextField="ItemName" DataValueField="ProductItemID" runat="server" CssClass="lidropdown">
                                        </asp:DropDownList>
                                <asp:ImageButton runat="server" ID="ShowHarddriveInfo" ImageUrl="/_Images/server_configurator_helpbutton.png"
                                    OnClick="lnkShowHarddriveInfo_OnClick" />

                                <asp:Label runat="server" ID="lbHardDrivesPrice" />

                    </ItemTemplate>
                    <FooterTemplate>

                    </FooterTemplate>
                </asp:Repeater>

for the backend I am trying to load a dynamic number of Dropdown lists into the repeater then databind them all with the same data.

  public void PopulateHardDrives(int intSupportedDrives)
  {
    PreloadHardDriveRepeater(intSupportedDrives);

    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["connstrname"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Prod_SelectIDNamePriceByCategory", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@CategoryCode", "Hard Drive");
    DataTable dtHardDrives = new DataTable();
    using (conn)
    {
      conn.Open();
      SqlDataReader dr = cmd.ExecuteReader();
      dtHardDrives.Load(dr);
      ViewState.Add("dtHardDrives", dtHardDrives);
    }

    foreach (RepeaterItem riHardDrive in RepeaterHardDrives.Items)
    {
      DropDownList ddHardDrives = (DropDownList)riHardDrive.FindControl("ddHardDrives");
      ddHardDrives.DataSource = dtHardDrives;//program gives NULL exception error here(object not set to instance of object however it know the count of the rows it is supposed to be pulling)
      ddHardDrives.DataValueField = "ProductItemID";
      ddHardDrives.DataTextField = "ItemName";
      ddHardDrives.DataBind();
      Label lbHardDrive = (Label)riHardDrive.FindControl("lbHardDrivesPrice");
      lbHardDrive.Text = String.Format("{0:c}", Convert.ToDecimal("0.00"));
      if (riHardDrive.ItemIndex != 0) //We do not want to allow None to be selected on the main drive
      {
        ddHardDrives.Items.Insert(0, "None");
      }
    }
  }

and last but not least the function to setup the dynamic amount of DDL's looks like this

     private void PreloadHardDriveRepeater(int intSupportedDrives)
    {
        int[] intArrDisks = new int[intSupportedDrives];
        for (int intDiskCount = 0; intDiskCount < intArrDisks.Length; intDiskCount++)
        {
            intArrDisks[intDiskCount] = intDiskCount;
        }
        RepeaterHardDrives.DataSource = intArrDisks;
        RepeaterHardDrives.DataBind();
    }

I am calling a list of populate functions in a !page.isPostBack if statement and the only one that is not getting the data is this one with the Drown Lists.  It gets the number of Rows(18) from the database, but it it throwing a Null error(Object reference not set to an instance of an object.) I have seen quite a few people have been running into this error while googling the problem, however I could not find a solution that worked for me. The PreloadHardDriveRepeater function seems to work fine when run alone it loads the correct amount of DDL's onto the page.

Thanks ahead of time.

Body:
Your control is "ddHardDrive":

<asp:DropDownList ID="ddHardDrive" DataTextField="ItemName" DataValueField="ProductItemID" runat="server" CssClass="lidropdown">

and your code is looking for "ddHardDrives"

riHardDrive.FindControl("ddHardDrives");

This would be easy to notice if you debugged into the function and looked at your variable values right before the exception is thrown.


问:
Where does mongodb stand in the CAP theorem?
Everywhere I look, I see that MongoDB is CP.
But when I dig in I see it is eventually consistent.
Is it CP when you use safe=true? If so, does that mean that when I write with safe=true, all replicas will be updated before getting the result?</p>

答:
I agree with Luccas post. You can't just say that MongoDB is CP/AP/CA, because it actually is a trade-off between C, A and P, depending on both database/driver configuration and type of disaster: here's a visual recap, and below a more detailed explanation.

Scenario
Main Focus
Description

No partition
CA
The system is available and provides strong consistency

partition, majority connected
AP
Not synchronized writes from the old primary are ignored

partition, majority not connected
CP
only read access is provided to avoid separated and inconsistent systems

Consistency:

MongoDB is strongly consistent when you use a single connection or the correct Write/Read Concern Level (Which will cost you execution speed). As soon as you don't meet those conditions (especially when you are reading from a secondary-replica) MongoDB becomes Eventually Consistent.

Availability:

MongoDB gets high availability through Replica-Sets. As soon as the primary goes down or gets unavailable else, then the secondaries will determine a new primary to become available again. There is an disadvantage to this: Every write that was performed by the old primary, but not synchronized to the secondaries will be rolled back and saved to a rollback-file, as soon as it reconnects to the set(the old primary is a secondary now). So in this case some consistency is sacrificed for the sake of availability.

Partition Tolerance:

Through the use of said Replica-Sets MongoDB also achieves the partition tolerance: As long as more than half of the servers of a Replica-Set is connected to each other, a new primary can be chosen. Why? To ensure two separated networks can not both choose a new primary. When not enough secondaries are connected to each other you can still read from them (but consistency is not ensured), but not write. The set is practically unavailable for the sake of consistency.

ContentLicense: CC BY-SA 3.0

ContentLicense: CC BY-SA 4.0


问:
How to get a key from custom keyboard, to fill 2 rows
I have made a custom keyboard on android for some numeric inputs,
in this keyboard I have an "OK" button and I want that button to fill 2 rows.

My xml looks like this:

```<code><?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="50%p" android:horizontalGap="1px"
    android:verticalGap="1px" android:keyHeight="40dip">
    <Row>
        <Key android:codes="8" android:keyLabel="1" android:keyEdgeFlags="left" />
        <Key android:codes="9" android:keyLabel="2" />
        <Key android:codes="10" android:keyLabel="3" />
        <Key android:codes="-5" android:keyIcon="@drawable/ic_delete_512" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:codes="11" android:keyLabel="4" android:keyEdgeFlags="left" />
        <Key android:codes="12" android:keyLabel="5" />
        <Key android:codes="13" android:keyLabel="6" />
        <Key android:codes="13" android:keyLabel="C" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:codes="14" android:keyLabel="7"  android:keyEdgeFlags="left" />
        <Key android:codes="15" android:keyLabel="8" />
        <Key android:codes="16" android:keyLabel="9" />
        <Key android:codes="-3" android:keyLabel="OK" />
    </Row>
    <Row>
        <Key android:codes="-1" android:keyLabel="00"  android:keyEdgeFlags="left" />
        <Key android:codes="0" android:keyLabel="0" />
        <Key android:codes="-4" android:keyLabel="#" />
    </Row>
</Keyboard>
</code>```

My objective is to have the bottom row to contain 3 columns, and the "OK" button, to fill 2 rows.

答:
Try the following:</p>

```<code><Row>
    <Key android:codes="14" android:keyLabel="7"  android:keyEdgeFlags="left" />
    <Key android:codes="15" android:keyLabel="8" />
    <Key android:codes="16" android:keyLabel="9" />
    <Key android:codes="-3" android:keyLabel="OK" android:keyHeight="80dip" />
</Row>
<Row>
    <Key android:codes="-1" android:keyLabel="00"  android:keyEdgeFlags="left" />
    <Key android:codes="0" android:keyLabel="0" />
    <Key android:codes="-4" android:keyLabel="#" android:horizontalGap="25%p" />
</Row>
</code>```

Body:
I have made a custom keyboard on android for some numeric inputs,
in this keyboard I have an "OK" button and I want that button to fill 2 rows.

My xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="50%p" android:horizontalGap="1px"
    android:verticalGap="1px" android:keyHeight="40dip">
    <Row>
        <Key android:codes="8" android:keyLabel="1" android:keyEdgeFlags="left" />
        <Key android:codes="9" android:keyLabel="2" />
        <Key android:codes="10" android:keyLabel="3" />
        <Key android:codes="-5" android:keyIcon="@drawable/ic_delete_512" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:codes="11" android:keyLabel="4" android:keyEdgeFlags="left" />
        <Key android:codes="12" android:keyLabel="5" />
        <Key android:codes="13" android:keyLabel="6" />
        <Key android:codes="13" android:keyLabel="C" android:keyEdgeFlags="right" />
    </Row>
    <Row>
        <Key android:codes="14" android:keyLabel="7"  android:keyEdgeFlags="left" />
        <Key android:codes="15" android:keyLabel="8" />
        <Key android:codes="16" android:keyLabel="9" />
        <Key android:codes="-3" android:keyLabel="OK" />
    </Row>
    <Row>
        <Key android:codes="-1" android:keyLabel="00"  android:keyEdgeFlags="left" />
        <Key android:codes="0" android:keyLabel="0" />
        <Key android:codes="-4" android:keyLabel="#" />
    </Row>
</Keyboard>

My objective is to have the bottom row to contain 3 columns, and the "OK" button, to fill 2 rows.

Body:
Try the following:

<Row>
    <Key android:codes="14" android:keyLabel="7"  android:keyEdgeFlags="left" />
    <Key android:codes="15" android:keyLabel="8" />
    <Key android:codes="16" android:keyLabel="9" />
    <Key android:codes="-3" android:keyLabel="OK" android:keyHeight="80dip" />
</Row>
<Row>
    <Key android:codes="-1" android:keyLabel="00"  android:keyEdgeFlags="left" />
    <Key android:codes="0" android:keyLabel="0" />
    <Key android:codes="-4" android:keyLabel="#" android:horizontalGap="25%p" />
</Row>


问:
Ask the user to enter an integer that is over 500. Work out the square root of that number and display it to 2 decimal places
Iv been asked to write a program wich asks the user to enter an integer that is over 500. Then I need to work out the square root of that number and display it to 2 decimal places.

So far I have:

```<code>import math
mumber= int(input("Please enter a number over 500")
if number<500:
       print ("That's too low")
else:
      print (math.sqrt(number))
</code>```

Everything works but where do I place the print(round(number,2)) ?

EDIT:
I typed the code, so YES the indenting is wrong.

答:
Make sure 'mumber' is fixed to say 'number'  and instead of

```<code>print(math.sqrt(number))
</code>```

use

```<code>print(round(math.sqrt(number),2))
</code>```

Body:
Iv been asked to write a program wich asks the user to enter an integer that is over 500. Then I need to work out the square root of that number and display it to 2 decimal places.

So far I have:

import math
mumber= int(input("Please enter a number over 500")
if number<500:
       print ("That's too low")
else:
      print (math.sqrt(number))

Everything works but where do I place the print(round(number,2)) ?

EDIT:
I typed the code, so YES the indenting is wrong.

Body:
Make sure 'mumber' is fixed to say 'number'  and instead of

print(math.sqrt(number))

use

print(round(math.sqrt(number),2))


问:
Merge fields of object to create new field with Ramda
I have a collection that I’d like to grab some values from, and merge them together to form a new value. So far I’ve looked at R.evolve, but with no luck (because medias is also a collection.

This is what I’m starting with:

```<code>[{
  "permalink": "http://example.com/1",
  "medias": [{
    "filename": "image_1.png",
  }]
}, {
  "permalink": "http://example.com/3",
  "medias": [{
    "filename": "image_3.png",
  }]
}]
</code>```

And this is what I’d like to end with:

```<code>[{
  "permalink": "http://example.com/1",
  "medias": [{
    "filename": "image_1.png",
    "image_url": "http://example.com/1/image_1.png"
  }]
}, {
  "permalink": "http://example.com/3",
  "medias": [{
    "filename": "image_3.png",
    "image_url": "http://example.com/3/image_3.png"
  }]
}]
</code>```

Essentially what we’re doing is prepending each filename with a permalink.

This is the kind of thing I’ve been going for so far, unfortunately when running the transform on medias, I can’t get access to the permalink property.

```<code>const mapMedia = R.map(
  // can’t get access to the `permalink` in here?
)

const transformations = {
  medias: mapMedia
}

const transform = R.compose(
  R.map(R.evolve(transformations)),
)(data)
</code>```

答:
So the problem is that the data you need is not in scope.  In my opinion, the easiest way to fix that is to add some scope!

First off, you don't need that compose call, since you're only supplying it one function.  So we can replace

```<code>const transform = R.compose(
  R.map(R.evolve(transformations)),
)(data)
</code>```

with just

```<code>const transform = R.map(R.evolve(transformations)),
</code>```

(I also removed the call with (data), since I'm trying to build this up as a reusable function.  We would then call it later with transform(data).

Now we have to add context so that transformations has the object in scope.  We can do that like this:

```<code>const transform = R.map(image => R.evolve(transformations(image), image));
</code>```

Of course that means transformations needs to change to take this into account.  We can do that like this:

```<code>const transformations = image => ({
  medias: mapMedia(image.permalink)
})
</code>```

... and this in turn requires a change to mapMedia.  I thought that it should only need to know about the permalink and not the entire image, so we can pass only that as above.  And mapMedia would now look like this:

```<code>const mapMedia = permalink => R.map(
  medium => // do something with `medium` and `permalink`
);
</code>```

There are many ways that you could do this last step.  One of them, fairly aligned to your use of evolve would be this:

```<code>const mapMedia = permalink => R.map(
  medium => assoc('image_url', permalink + '/' + medium.filename, medium)
);
</code>```

Another alternative to both evolve and assoc is to investigate the various lens-related functions in Ramda, such as lensProp, over, and set.

Now, if those helper functions are only used for this purpose, you might not want them cluttering things up, and you could choose to inline them in your main function.  If you liked, you might combine all the above into

```<code>const addUrls = map(
  image => evolve({
    medias: map(medium => assoc('image_url', image.permalink + '/' + medium.filename, medium)),
  }, image)
);
</code>```

You can see the separate functions approach or the combined one in the Ramda REPL.  For good measure, there's also a version using lenses.

(And on a separate note, the word "media" is already plural.  The singular is "medium".  There's no good reason for "medias".)

Body:
I have a collection that I’d like to grab some values from, and merge them together to form a new value. So far I’ve looked at R.evolve, but with no luck (because medias is also a collection.

This is what I’m starting with:

[{
  "permalink": "http://example.com/1",
  "medias": [{
    "filename": "image_1.png",
  }]
}, {
  "permalink": "http://example.com/3",
  "medias": [{
    "filename": "image_3.png",
  }]
}]

And this is what I’d like to end with:

[{
  "permalink": "http://example.com/1",
  "medias": [{
    "filename": "image_1.png",
    "image_url": "http://example.com/1/image_1.png"
  }]
}, {
  "permalink": "http://example.com/3",
  "medias": [{
    "filename": "image_3.png",
    "image_url": "http://example.com/3/image_3.png"
  }]
}]

Essentially what we’re doing is prepending each filename with a permalink.

This is the kind of thing I’ve been going for so far, unfortunately when running the transform on medias, I can’t get access to the permalink property.

const mapMedia = R.map(
  // can’t get access to the `permalink` in here?
)

const transformations = {
  medias: mapMedia
}

const transform = R.compose(
  R.map(R.evolve(transformations)),
)(data)

Body:
So the problem is that the data you need is not in scope.  In my opinion, the easiest way to fix that is to add some scope!

First off, you don't need that compose call, since you're only supplying it one function.  So we can replace

const transform = R.compose(
  R.map(R.evolve(transformations)),
)(data)

with just

const transform = R.map(R.evolve(transformations)),

(I also removed the call with (data), since I'm trying to build this up as a reusable function.  We would then call it later with transform(data).

Now we have to add context so that transformations has the object in scope.  We can do that like this:

const transform = R.map(image => R.evolve(transformations(image), image));

Of course that means transformations needs to change to take this into account.  We can do that like this:

const transformations = image => ({
  medias: mapMedia(image.permalink)
})

... and this in turn requires a change to mapMedia.  I thought that it should only need to know about the permalink and not the entire image, so we can pass only that as above.  And mapMedia would now look like this:

const mapMedia = permalink => R.map(
  medium => // do something with `medium` and `permalink`
);

There are many ways that you could do this last step.  One of them, fairly aligned to your use of evolve would be this:

const mapMedia = permalink => R.map(
  medium => assoc('image_url', permalink + '/' + medium.filename, medium)
);

Another alternative to both evolve and assoc is to investigate the various lens-related functions in Ramda, such as lensProp, over, and set.

Now, if those helper functions are only used for this purpose, you might not want them cluttering things up, and you could choose to inline them in your main function.  If you liked, you might combine all the above into

const addUrls = map(
  image => evolve({
    medias: map(medium => assoc('image_url', image.permalink + '/' + medium.filename, medium)),
  }, image)
);

You can see the <a href="http://ramdajs.com/repl/?v=0.24.1#?const%20coll%20%3D%20%5B%7B%0A%20%20%22permalink%22%3A%20%22http%3A%2F%2Fexample.com%2F1%22%2C%0A%20%20%22medias%22%3A%20%5B%7B%0A%20%20%20%20%22filename%22%3A%20%22image_1.png%22%2C%0A%20%20%7D%2C%20%7B%0A%20%20%20%20%22filename%22%3A%20%22image_1.svg%22%2C%0A%20%20%7D%5D%0A%7D%2C%20%7B%0A%20%20%22permalink%22%3A%20%22http%3A%2F%2Fexample.com%2F3%22%2C%0A%20%20%22medias%22%3A%20%5B%7B%0A%20%20%20%20%22filename%22%3A%20%22image_3.png%22%2C%0A%20%20%7D%5D%0A%7D%5D%3B%0A%0Aconst%20mapMedia%20%3D%20permalink%20%3D%3E%20R.map%28%0A%20%20medium%20%3D%3E%20assoc%28%27image_url%27%2C%20permalink%20%2B%20%27%2F%27%20%2B%20medium.filename%2C%20medium%29%0A%29%3B%0A%0Aconst%20transformations%20%3D%20image%20%3D%3E%20%28%7B%0A%20%20medias%3A%20mapMedia%28image.permalink%29%0A%7D%29%0A%0Aconst%20transform%20%3D%20R.map%28image%20%3D%3E%20R.evolve%28transformations%28image%29%2C%20image%29%29%3B%0A%0Atransform%28coll%29" rel="nofollow noreferrer">separate functions approach or the <a href="http://ramdajs.com/repl/?v=0.24.0#?const%20coll%20%3D%20%5B%7B%0A%20%20%22permalink%22%3A%20%22http%3A%2F%2Fexample.com%2F1%22%2C%0A%20%20%22medias%22%3A%20%5B%7B%0A%20%20%20%20%22filename%22%3A%20%22image_1.png%22%2C%0A%20%20%7D%2C%20%7B%0A%20%20%20%20%22filename%22%3A%20%22image_1.svg%22%2C%0A%20%20%7D%5D%0A%7D%2C%20%7B%0A%20%20%22permalink%22%3A%20%22http%3A%2F%2Fexample.com%2F3%22%2C%0A%20%20%22medias%22%3A%20%5B%7B%0A%20%20%20%20%22filename%22%3A%20%22image_3.png%22%2C%0A%20%20%7D%5D%0A%7D%5D%3B%0A%0Aconst%20addUrls%20%3D%20map%28%0A%20%20image%20%3D%3E%20evolve%28%7B%0A%20%20%20%20medias%3A%20map%28medium%20%3D%3E%20assoc%28%27image_url%27%2C%20image.permalink%20%2B%20%27%2F%27%20%2B%20medium.filename%2C%20medium%29%29%2C%0A%20%20%7D%2C%20image%29%0A%29%3B%0A%0AaddUrls%28coll%29%3B%0A" rel="nofollow noreferrer">combined one in the Ramda REPL.  For good measure, there's also a <a href="http://ramdajs.com/repl/?v=0.24.0#?const%20coll%20%3D%20%5B%7B%0A%20%20%22permalink%22%3A%20%22http%3A%2F%2Fexample.com%2F1%22%2C%0A%20%20%22medias%22%3A%20%5B%7B%0A%20%20%20%20%22filename%22%3A%20%22image_1.png%22%2C%0A%20%20%7D%2C%20%7B%0A%20%20%20%20%22filename%22%3A%20%22image_1.svg%22%2C%0A%20%20%7D%5D%0A%7D%2C%20%7B%0A%20%20%22permalink%22%3A%20%22http%3A%2F%2Fexample.com%2F3%22%2C%0A%20%20%22medias%22%3A%20%5B%7B%0A%20%20%20%20%22filename%22%3A%20%22image_3.png%22%2C%0A%20%20%7D%5D%0A%7D%5D%3B%0A%0Aconst%20addUrls%20%3D%20map%28%0A%20%20image%20%3D%3E%20over%28%0A%20%20%20%20lensProp%28%27medias%27%29%2C%20%0A%20%20%20%20map%28medium%20%3D%3E%20set%28lensProp%28%27image_url%27%29%2C%20image.permalink%20%2B%20%27%2F%27%20%2B%20medium.filename%2C%20medium%29%29%2C%0A%20%20%20%20image%0A%20%20%29%0A%29%3B%0A%0AaddUrls%28coll%29%3B%0A" rel="nofollow noreferrer">version using lenses.

(And on a separate note, the word "media" is already plural.  The singular is "medium".  There's no good reason for "medias".)


问:
Handling scala array group with ties
I have an array of names. I need to group by elements and select the name with highest count. If there are ties, then return the alphabetically last name. I have the below so far:

```<code>val names = Array("Adam", "Eve", "Adam", "Eve", "John", "Doe")
val countNames = names.map(x => (x, names.count(_ == x))).toSeq.sortBy(_._2)
</code>```

My result should be Eve. How can I get that?

答:
Define a lexicographical order, where the count has priority over the string. Then use it to sort your list and pick the first element:</p>

```<code> countNames.sortWith( (x,y) =>
                       (x._2 > y._2) ||
                       (x._2 == y._2 && x._1 > y._1)  )(0)._2
</code>```

Title: Handling scala array group with ties

Body:
I have an array of names. I need to group by elements and select the name with highest count. If there are ties, then return the alphabetically last name. I have the below so far:

val names = Array("Adam", "Eve", "Adam", "Eve", "John", "Doe")
val countNames = names.map(x => (x, names.count(_ == x))).toSeq.sortBy(_._2)

My result should be Eve. How can I get that?

Body:
Define a lexicographical order, where the count has priority over the string. Then use it to sort your list and pick the first element:

 countNames.sortWith( (x,y) =>
                       (x._2 > y._2) ||
                       (x._2 == y._2 && x._1 > y._1)  )(0)._2


问:
How to validate one dropdown if the other dropdown is not selected and vice versa?
I have a form with 2 multi select dropdowns for list of country and list of sites....

I do form validation using jQuery Validate plugin....

scenario to achieve: if one dropdown is selected then i want the form to be submitted irrespective of second dropdown selected or not....

current scenario of issue I face: even if one dropdown is selected form is not submitting..form submits only if second dropdown is selected

I came across the depends attribute inside validate function..but it is not working either....

I am trying it for my 2 dropdowns with ids list_country and list_site

below is my code:

```<code>$('#feed_form').validate({ // initialize the plugin
        rules: {
            list_country: {
                required: {
                    depends: function () { return $('#list_site').val() == null }
                }
            },
            list_site: {
                required: {
                    depends: function () { return $('#list_country').val() == null }
                }
            },
            list_trend: {
                required: true
            }
        },
        ignore: ":hidden:not(select)"
    });
</code>```

form markup (source code as it is written in YII framework in php)

```<code><form name="feed_form" id="feed_form" action="../minUser/DisplayUsers" method="post">       <div class="row">
            <div class="span3"><strong>Country:</strong></div>
            <div class="span3">
                   <select class="required multi_select" multiple="multiple" style="width:100%" onchange="js:$("#list_country_chzn").removeClass("error_container");" name="list_country[]" id="list_country">
<option value="666666">All</option>
<option value="5">xyz1</option>
<option value="5">xyz2</option>
<option value="5">xyz3</option>
<option value="5">xyz4</option>
<option value="5">xyz5</option>
<option value="5">xyz6</option>
</select>           </div>
        </div>
        <div class="row">
            <div class="span3"><strong>Site:</strong></div>
            <div class="span3">
                   <select class="required multi_select" multiple="multiple" style="width:100%" onchange="js:$("#list_site_chzn").removeClass("error_container");" name="list_site[]" id="list_site">
<option value="666666">All</option>
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="4">d</option>
</select>            </div>
        </div>
        <div class="row">
            <div class="span3"><strong>Trend:</strong></div>
            <div class="span3">
                   <select class="required" style="width:100%" name="list_trend" id="list_trend">
<option value="">--Select Trend--</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>            </div>
        </div>
        <div class="row">
            <div class="span3">
            <div class="row buttons" align="left" style="margin-top:15px;">
                <input name="search_feed_form" class="btn btn-info" onclick="return checkVerifyForm();" type="submit" value="Search" />            </div>
            </div>
        </div>
    </div>
<!--end outermost box widget-->
</form>
</code>```

答:
scenario to achieve: if one dropdown is selected then I want the form to be submitted irrespective of second dropdown selected or not.

You would want the require_from_group method, which is part of the Additional Methods file.  This rule, when properly assigned to both select elements, would simply ensure that at least one is selected.

<s>I would construct a working demo, but you have not shown us any of your HTML.</s>

You need to remove the required class as that will force the required rule and over-ride your depends. In the case of mine, it adds the required rule, which we do not want here.

Using the rules object, you must target the field's name, not the id.

Finally, instead of depends, you can use the require_from_group method to ensure only one select is required out of the two.

DEMO: jsfiddle.net/376otgfL/

```<code>$('#feed_form').validate({ // initialize the plugin
    rules: {
        'list_country[]': {  // <- NAME, not id
            require_from_group: [1,".multi_select"]
        },
        'list_site[]': {     // <- NAME, not id
            require_from_group: [1,".multi_select"]
        },
        list_trend: {  // <- NAME, not id
            required: true
        }
    },
    ignore: ":hidden:not(select)"
});
</code>```

Body:
I have a form with 2 multi select dropdowns for list of country and list of sites....

I do form validation using jQuery Validate plugin....

scenario to achieve: if one dropdown is selected then i want the form to be submitted irrespective of second dropdown selected or not....

current scenario of issue I face: even if one dropdown is selected form is not submitting..form submits only if second dropdown is selected

I came across the depends attribute inside validate function..but it is not working either....

I am trying it for my 2 dropdowns with ids list_country and list_site

below is my code:

$('#feed_form').validate({ // initialize the plugin
        rules: {
            list_country: {
                required: {
                    depends: function () { return $('#list_site').val() == null }
                }
            },
            list_site: {
                required: {
                    depends: function () { return $('#list_country').val() == null }
                }
            },
            list_trend: {
                required: true
            }
        },
        ignore: ":hidden:not(select)"
    });

form markup (source code as it is written in YII framework in php)

<form name="feed_form" id="feed_form" action="../minUser/DisplayUsers" method="post">

Country:

                   <select class="required multi_select" multiple="multiple" style="width:100%" onchange="js:$("#list_country_chzn").removeClass("error_container");" name="list_country[]" id="list_country">
<option value="666666">All</option>
<option value="5">xyz1</option>
<option value="5">xyz2</option>
<option value="5">xyz3</option>
<option value="5">xyz4</option>
<option value="5">xyz5</option>
<option value="5">xyz6</option>
</select>

Site:

                   <select class="required multi_select" multiple="multiple" style="width:100%" onchange="js:$("#list_site_chzn").removeClass("error_container");" name="list_site[]" id="list_site">
<option value="666666">All</option>
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="4">d</option>
</select>

Trend:

                   <select class="required" style="width:100%" name="list_trend" id="list_trend">
<option value="">--Select Trend--</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>

                <input name="search_feed_form" class="btn btn-info" onclick="return checkVerifyForm();" type="submit" value="Search" />

</form>

Body:
scenario to achieve: if one dropdown is selected then I want the form to be submitted irrespective of second dropdown selected or not.

You would want the require_from_group method, which is part of the Additional Methods file.  This rule, when properly assigned to both select elements, would simply ensure that at least one is selected.

<s>I would construct a working demo, but you have not shown us any of your HTML.</s>

You need to remove the required class as that will force the required rule and over-ride your depends. In the case of mine, it adds the required rule, which we do not want here.

Using the rules object, you must target the field's name, not the id.

Finally, instead of depends, you can use the require_from_group method to ensure only one select is required out of the two.

DEMO: jsfiddle.net/376otgfL/

$('#feed_form').validate({ // initialize the plugin
    rules: {
        'list_country[]': {  // <- NAME, not id
            require_from_group: [1,".multi_select"]
        },
        'list_site[]': {     // <- NAME, not id
            require_from_group: [1,".multi_select"]
        },
        list_trend: {  // <- NAME, not id
            required: true
        }
    },
    ignore: ":hidden:not(select)"
});


问:
Enable Android Studio spell checking on language specific strings.xml files
In Android Studio, spellchecking seems to be disabled for language specific strings.xml files (and I understand why it would be disabled by default).

But is it possible to enable it, to check values, for a couple of those files? ("fr/strings.xml" for example), or at least run it on demand.

答:
To avoid or to enforce spellchecking you could add tools:locale to the <resources> tag in your string xml.

```<code><resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">
</code>```

This tells Lint your resources' locale.

Source

Body:
To avoid or to enforce spellchecking you could add tools:locale to the <resources> tag in your string xml.

<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">

This tells Lint your resources' locale.

Source


问:
How to combine two collections in mongodb?
I want to combine two collections ("messages" and "date").
The collection "messages" contains documents like this one:

```<code>{
    "_id" : ObjectId("4f16fc97d1e2d32371003e27"),
    "body" : "the scrimmage is still up in the air...\n\n\nwebb said that they didnt want to scrimmage...\n\nthe aggies  are scrimmaging each other... (the aggie teams practiced on \nSunday)\n\nwhen I called the aggie captains to see if we could use their field.... they \nsaid that it was tooo smalll for us to use...\n\n\nsounds like bullshit to me... but what can we do....\n\n\nanyway... we will have to do another practice Wed. night....    and I dont' \nknow where we can practice.... any suggestions...\n\n\nalso,  we still need one  more person...",
    "subFolder" : "notes_inbox",
    "mailbox" : "bass-e",
    "filename" : "450.",
    "X-cc" : "",
    "From" : "michael.simmons@enron.com",
    "Subject" : "Re: Plays and other information",
    "X-Folder" : "\\Eric_Bass_Dec2000\\Notes Folders\\Notes inbox",
    "Content-Transfer-Encoding" : "7bit",
    "X-bcc" : "",
    "To" : "eric.bass@enron.com",
    "X-Origin" : "Bass-E",
    "X-FileName" : "ebass.nsf",
    "X-From" : "Michael Simmons",
    "Date" : "Tue, 14 Nov 2000 08:22:00 -0800 (PST)",
    "X-To" : "Eric Bass",
    "Message-ID" : "<6884142.1075854677416.JavaMail.evans@thyme>",
    "Content-Type" : "text/plain; charset=us-ascii",
    "Mime-Version" : "1.0",
}
</code>```

The collection "date" contains documents like this one:

```<code>{
    "_id" : ObjectId("4f16fc97d1e2d32371003e27"),
    "year" : NumberInt(2000),
    "month" : NumberInt(11),
    "day" : NumberInt(14)
}
</code>```

The day, the month and the year should be inserted into the collection messages. I have tried different approaches but have not come to any solution.

An example of what I've tried:

```<code>db.messages.aggregate([
    {
        $lookup:
        {
            from: "date",
            localField: "Date",
            foreignField: "year",
            as: "Year"

        }
    }
])
</code>```

答:
The problem as the user @Vanojx suggested in his comment,

you are matching "Tue, 14 Nov 2000 08:22:00 -0800 (PST)" with "2000"

However, why is the Date field in messages collection in RFC format and stored as a string? It should have been stored as a Date object in ISO format.

According to this SO post, aggregation framework doesn't operate in the v8 engine and you won't have JS functions to do manipulation on the string.

Besides manipulating the string to extract "2000" out of it, to be later used to join on the "date" collection doesn't sound like a right approach to me.

If possible please convert it from string to Date object. To convert date field to Date object you could do this,

```<code>//please make back up of your db just incase
db.messages.find({}).forEach(function (doc) {
    doc.Date = new Date(doc.Date);
    db.messages.save(doc);
});
</code>```

Once you convert your "Date" field from string to a Date Object, following query should work for you.

```<code>db.messages.aggregate([
    {
        $project: {
            body: 1,
            subFolder: 1,
            Date: 1,
            //add other properties as you need
            //you could even do following which will give you all fields of the document
            //document: "$$ROOT"

            year: {$year: "$Date"}
        }
    },
    {
        $lookup: {
            from: "date",
            localField: "year",
            foreignField: "year",
            as: "Year"
        }
    }
    ])
</code>```

Here we first extract the year out of the Date field using $year. Then we will join using the $lookup on that field.

I hope this helps.

Body:
I want to combine two collections ("messages" and "date").
The collection "messages" contains documents like this one:

{
    "_id" : ObjectId("4f16fc97d1e2d32371003e27"),
    "body" : "the scrimmage is still up in the air...\n\n\nwebb said that they didnt want to scrimmage...\n\nthe aggies  are scrimmaging each other... (the aggie teams practiced on \nSunday)\n\nwhen I called the aggie captains to see if we could use their field.... they \nsaid that it was tooo smalll for us to use...\n\n\nsounds like bullshit to me... but what can we do....\n\n\nanyway... we will have to do another practice Wed. night....    and I dont' \nknow where we can practice.... any suggestions...\n\n\nalso,  we still need one  more person...",
    "subFolder" : "notes_inbox",
    "mailbox" : "bass-e",
    "filename" : "450.",
    "X-cc" : "",
    "From" : "michael.simmons@enron.com",
    "Subject" : "Re: Plays and other information",
    "X-Folder" : "\\Eric_Bass_Dec2000\\Notes Folders\\Notes inbox",
    "Content-Transfer-Encoding" : "7bit",
    "X-bcc" : "",
    "To" : "eric.bass@enron.com",
    "X-Origin" : "Bass-E",
    "X-FileName" : "ebass.nsf",
    "X-From" : "Michael Simmons",
    "Date" : "Tue, 14 Nov 2000 08:22:00 -0800 (PST)",
    "X-To" : "Eric Bass",
    "Message-ID" : "<6884142.1075854677416.JavaMail.evans@thyme>",
    "Content-Type" : "text/plain; charset=us-ascii",
    "Mime-Version" : "1.0",
}

The collection "date" contains documents like this one:

{
    "_id" : ObjectId("4f16fc97d1e2d32371003e27"),
    "year" : NumberInt(2000),
    "month" : NumberInt(11),
    "day" : NumberInt(14)
}

The day, the month and the year should be inserted into the collection messages. I have tried different approaches but have not come to any solution.

An example of what I've tried:

db.messages.aggregate([
    {
        $lookup:
        {
            from: "date",
            localField: "Date",
            foreignField: "year",
            as: "Year"

        }
    }
])

Body:
The problem as the user @Vanojx suggested in his comment,

you are matching "Tue, 14 Nov 2000 08:22:00 -0800 (PST)" with "2000"

However, why is the Date field in messages collection in RFC format and stored as a string? It should have been stored as a Date object in ISO format.

According to this SO post, aggregation framework doesn't operate in the v8 engine and you won't have JS functions to do manipulation on the string.

Besides manipulating the string to extract "2000" out of it, to be later used to join on the "date" collection doesn't sound like a right approach to me.

If possible please convert it from string to Date object. To convert date field to Date object you could do this,

//please make back up of your db just incase
db.messages.find({}).forEach(function (doc) {
    doc.Date = new Date(doc.Date);
    db.messages.save(doc);
});

Once you convert your "Date" field from string to a Date Object, following query should work for you.

db.messages.aggregate([
    {
        $project: {
            body: 1,
            subFolder: 1,
            Date: 1,
            //add other properties as you need
            //you could even do following which will give you all fields of the document
            //document: "$$ROOT"

            year: {$year: "$Date"}
        }
    },
    {
        $lookup: {
            from: "date",
            localField: "year",
            foreignField: "year",
            as: "Year"
        }
    }
    ])

Here we first extract the year out of the Date field using $year. Then we will join using the $lookup on that field.

I hope this helps.


问:
Error: Finish can only be called once
I have a problem with Gradle building. When I build my project, it displays the error:

Finish can only be called once in android studio

Anyone have idea about this error?

答: There are many sophisticated answers. But for me none of this helped, only reboot. </p>

Title: Error: Finish can only be called once


问:
Auto incrementing ID as primary key on all tables
I am designing a database for an e-Commerce website which uses MySQL. I have made the list of tables necessary and all the fields necessary for the table. I have a total of 9 tables.

What I have done is include an auto incrementing ID as primary key on all the tables.

All my tables except 2 are normalized to 3NF. Two tables 'users' and 'outlets' are not normalized to 2NF.

Along the way I realized that normalization is cumbersome when using auto incrementing ID as a primary key. As normalization is not strictly required, I want to know if there are any downsides of using auto incrementing ID as primary key on all the tables?

答:
I recommend using GUID - uniqueidentifier as a Primary Key to overrule any limitations in future.

MSSQL: https://learn.microsoft.com/en-us/sql/t-sql/data-types/uniqueidentifier-transact-sql

MySQL:
https://forums.asp.net/t/1458664.aspx?UNIQUEIDENTIFIER+data+type+in+MySql

http://mysqlserverteam.com/storing-uuid-values-in-mysql-tables/


问:
unexpected word boundaries behavior in regex
```<code>import re
text = 'washington d.c. columbia'
templ_1 = 'washington d.c'
templ_2 = 'washington d.c.'
print(re.sub(r'\b%s\b'%templ_1, '', text))
</code>```

I got . columbia as expected

```<code>print(re.sub(r'\b%s\b'%templ_2, '', text))
</code>```

I got washington d.c. columbia instead of . columbia How to fix it?

答:
\b describes a transition from word to non-word character. A . followed by a  is not such a transition (neither is a word character). In addition, you need to re.escape your strings being added to a regex, or the .s will match any character, not literal .s.

A possible fix would be:

```<code>print(re.sub(r'\b%s(?=\s|$)'%re.escape(templ_2), '', text))
</code>```

using a lookahead assertion that your pattern is followed by a space or the end of string, not a word boundary (since your pattern might not end in a word character). Using \W instead of \s is also reasonable.

Body:
import re
text = 'washington d.c. columbia'
templ_1 = 'washington d.c'
templ_2 = 'washington d.c.'
print(re.sub(r'\b%s\b'%templ_1, '', text))

I got . columbia as expected

print(re.sub(r'\b%s\b'%templ_2, '', text))

I got washington d.c. columbia instead of . columbia How to fix it?

Body:
\b describes a transition from word to non-word character. A . followed by a  is not such a transition (neither is a word character). In addition, you need to re.escape your strings being added to a regex, or the .s will match any character, not literal .s.

A possible fix would be:

print(re.sub(r'\b%s(?=\s|$)'%re.escape(templ_2), '', text))

using a lookahead assertion that your pattern is followed by a space or the end of string, not a word boundary (since your pattern might not end in a word character). Using \W instead of \s is also reasonable.


问:
Maven-Spring project doesn't load spring-boot-start archives
I've tried to run my maven based spring proyect locally with no pom modifications but it doesn't work, I got the following error (replace ${IP} with http://172.24.14.196:8888 because of my rank can't post more than 2 links):

[INFO] Scanning for projects...
  Downloading: ${IP}/archiva/repository/internal/org/springframework/boot/spring-boot-starter-parent/2.0.0.BUILD-SNAPSHOT/maven-metadata.xml
  [WARNING] Could not transfer metadata org.springframework.boot:spring-boot-starter-parent:2.0.0.BUILD-SNAPSHOT/maven-metadata.xml from/to archiva.central (${IP}172.24.14.196:8888/archiva/repository/internal/): Connect to 172.24.14.196:8888 [/172.24.14.196] failed: Connection timed out: connect
  Downloading: ${IP}/archiva/repository/internal/org/springframework/boot/spring-boot-starter-parent/2.0.0.BUILD-SNAPSHOT/spring-boot-starter-parent-2.0.0.BUILD-SNAPSHOT.pom
  [ERROR] The build could not read 1 project -> [Help 1]
  [ERROR]
  [ERROR]   The project co.com.spicy:Shapers:0.0.1-SNAPSHOT (E:\shapers\pom.xml) has 1 error
  [ERROR]     Non-resolvable parent POM: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.0.0.BUILD-SNAPSHOT from/to archiva.central (${IP}/archiva/repository/internal/): Connect to 172.24.14.196:8888 [/172.24.14.196] failed: Connection timed out: connect and 'parent.relativePath' points at no local POM @ line 14, column 13 -> [Help 2]
  [ERROR]
  [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  [ERROR]
  [ERROR] For more information about the errors and possible solutions, please read the following articles:
  [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
  [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

It looks like the spring-boot-starter its is getting no connection from the archiva central and cannot load resources, how can I manually load this resources or what is my problem?

I'm using the same configuration given here:

https://projects.spring.io/spring-boot/

答:
If spring-boot dependencies are missing in archiva you can download them from spring repositories.

Add this to pom.xml

```<code><repositories>

    <repository>
        <id>spring-repo</id>
        <name>Spring Repository</name>
        <url>https://repo.spring.io/release</url>
    </repository>

    <repository>
        <id>spring-milestone</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/libs-milestone</url>
    </repository>

    <repository>
        <id>spring-snapshot</id>
        <name>Spring Snapshot Repository</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
   </repository>

</repositories>
</code>```

Body:
If spring-boot dependencies are missing in archiva you can download them from spring repositories.

Add this to pom.xml

<repositories>

    <repository>
        <id>spring-repo</id>
        <name>Spring Repository</name>
        <url>https://repo.spring.io/release</url>
    </repository>

    <repository>
        <id>spring-milestone</id>
        <name>Spring Milestones</name>
        <url>http://repo.spring.io/libs-milestone</url>
    </repository>

    <repository>
        <id>spring-snapshot</id>
        <name>Spring Snapshot Repository</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
   </repository>

</repositories>