0

Помогите понять в чем проблема и как исправить. Для сохранения данных введенных в форму используется следующий код:

$('[name *= "pdata_ch_"]').each(function () {
	if (sessionStorage.getItem(this.name)) {
	this.value = sessionStorage.getItem(this.name);
}
	switch (this.type) {
			case 'text':
				this.addEventListener("keyup", function() {
					sessionStorage.setItem(this.name, this.value);
					
					console.log("---- text ----");
				});
				break;
			case 'radio':
				this.addEventListener("change", function() {

					sessionStorage.setItem(this.name, this.value);
					console.log("---- radio ----");
					console.log(this.value);
				});
				break;
			case 'select-one':
				this.addEventListener("change", function() {
					sessionStorage.setItem(this.name, this.value);
					
					console.log("---- select-one ----");
				});
				break;
			case 'checkbox':
				this.addEventListener("click", function() {
					console.log(this.type);
					sessionStorage.setItem(this.name, this.value);
					console.log("---- checkbox ----");
					console.log(this.value);
				});
				break;
			default :
			this.addEventListener("keyup", function() {
				console.log(this.type);
					sessionStorage.setItem(this.name, this.value);
					
					console.log("---- default ----");
				});
				break;
			}
});

Проблема с checkbox и radio, почему-то подменяется value (становится одинаковым) и не ставится checked.
Изначально:

<div class="row-fluid">
	<div class="fabrikgrid_radio span1">
		<label class="fabrikgrid_0 radio">
			<input type="radio" class="fabrikinput " name="pdata_ch___gender[]" value="0">
			<span>М</span>
		</label>
	</div>
	<div class="fabrikgrid_radio span1">
		<label class="fabrikgrid_1 radio">
			<input type="radio" class="fabrikinput " name="pdata_ch___gender[]" value="1">
			<span>Ж</span>
		</label>
	</div>
</div>

После обновления страницы:

<div class="row-fluid">
	<div class="fabrikgrid_radio span1">
		<label class="fabrikgrid_0 radio">
			<input type="radio" class="fabrikinput " name="pdata_ch___gender[]" value="0">
			<span>М</span>
		</label>
	</div>
	<div class="fabrikgrid_radio span1">
		<label class="fabrikgrid_1 radio">
			<input type="radio" class="fabrikinput " name="pdata_ch___gender[]" value="0">
			<span>Ж</span>
		</label>
	</div>
</div>

1 ответ 1

0

Решил таким способом:

if (sessionStorage.getItem(this.name) && this.type != 'radio' && this.type != 'checkbox') {
		this.value = sessionStorage.getItem(this.name);
}else if(sessionStorage.getItem(this.name) && (this.type == 'radio' || this.type == 'checkbox')){

   var ses_name = this.name;
   var ses_val = sessionStorage.getItem(this.name);
    $('[name="'+ ses_name +'"][value="'+ ses_val +'"]').attr('checked','checked');
}

Осталась проблема с зависимыми селектами, которые поочередно загружаются из БД.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.