Currently I have four separate sections: Sole, Collar, Lining and Tongue. In each section there is the option of selecting a material, the code below is an example of two of the sections.
<div class="sole_panel container active">
<h3>sole</h3>
<div id="sole_leather" class="material-btn">leather</div>
<div id="sole_suade" class="material-btn">suade</div>
<div id="sole_nubuck" class="material-btn">nubuck</div>
</div>
<div class="collar_panel container">
<h3>collar</h3>
<div id="collar_leather" class="material-btn">leather</div>
<div id="collar_suade" class="material-btn">suade</div>
<div id="collar_nubuck" class="material-btn">nubuck</div>
</div>
I have created the divs so that when they are clicked on it takes the id and passes it to the value of a hidden input tag.
<input id="sole" class="material-value" type="hidden" value="sole_leather">
The problem I am having is that if I select leather for the Sole section for example it populates all four hidden inputs like in the code below. Is there a simple way to have the hidden input only apply to the section it relates to?
<div class="sole_panel container active"></div>
<input id="sole" class="material-value" type="hidden" value="sole_leather">
<div class="collar_panel container"></div>
<input id="collar" class="material-value" type="hidden" value="sole_leather">
<div class="lining_panel container"></div>
<input id="lining" class="material-value" type="hidden" value="sole_leather">
<div class="tongue_panel container"></div>
<input id="tongue" class="material-value" type="hidden" value="sole_leather">
I’m also using JavaScript to loop over an array of panels and materials
panelArray = ['sole','collar','lining','tongue'];
materialArray = ['leather','suade','nubuck'];
//loop over array to create selectors
for (x = 0; x < panelArray.length; x++) {
mtr = '';
clr = '';
var segments = panelArray[x];
//loop over materials
for (y = 0; y < materialArray.length; y++) {
var material = materialArray[y];
//console.log(material);
mtr += '<div class="material-btn" id="' + segments + '_' + material + '">' + material + '</div>';
}
container += '<div class="' + segments + '_panel container"><h3>' + segments + '</h3>';
container += mtr;
container += '<div class="colour-container">';
container += clr;
container += '</div>';
container += '</div>';
container += '<input id="' + segments + '" class="material-value" type="hidden">';
}
below is the code that passes the value to the hidden field
$('.material-btn').on('click', function() {
$('.material-btn').removeClass('select');
$(this).addClass('select');
// passes material type selction to hidden field
var value = $(this).attr('id');
var result = $('.material-value').attr('value', value);
//console.log(result);
});
答案 0 :(得分:2)
试试这个:
$('.material-btn').click(function(){
var part = this.id.split('_')[0];
var matl = this.id.split('_')[1];
$('#'+part).val(matl);
});
注意:
(1)仅使用this.id
(纯javascript)代替$(this).attr('id')
,因为它更快,更容易输入。对于这个例子,他们将工作相同。要对所有jQuery执行完全相同的操作:var matl = $(this).attr('id').split('_')[1];
(2)this.id.split('_')[1]
获取id,例如sole_leather
,并将其转换为数组,在每个_
字符处拆分字符串。因此,您将得到如下数组:array('sole','leather')
- 而[1]
选择位置1处的元素(皮革)
(3)要将材料(正绒面革)保存到零件(鞋底)隐藏字段中,我们使用在步骤2中创建的存储变量。$('#'+part)
变为$('#sole')
- 这是唯一的隐藏领域。
(4)在jsFiddle演示中,隐藏的字段仅被取消隐藏,以便您可以在字段内看到正在更新的值。
/*
http://stackoverflow.com/questions/30038608/how-would-i-go-about-saving-four-separate-values-to-four-hidden-fields
*/
$('.material-btn').click(function(){
var part = this.id.split('_')[0];
var matl = this.id.split('_')[1];
$('#'+part).val(matl);
});
&#13;
#hiddenDIVs{width:80%;margin:30px auto;border:1px solid orange;}
.material-value{height:25px;width:60%;margin:5px auto;background:palegoldenrod;padding:2px 5px;}
.material-btn:hover{cursor:pointer;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="sole_panel container active">
<h3>sole</h3>
<div id="sole_leather" class="material-btn">leather</div>
<div id="sole_suade" class="material-btn">suade</div>
<div id="sole_nubuck" class="material-btn">nubuck</div>
</div>
<div class="collar_panel container">
<h3>collar</h3>
<div id="collar_leather" class="material-btn">leather</div>
<div id="collar_suade" class="material-btn">suade</div>
<div id="collar_nubuck" class="material-btn">nubuck</div>
</div>
<div class="sole_panel container active"></div>
<div class="collar_panel container"></div>
<div class="lining_panel container"></div>
<div class="tongue_panel container"></div>
<div id="hiddenDIVs">
<input id="sole" class="material-value" type="text" value="" /> SOLE<br>
<input id="collar" class="material-value" type="text" value="" /> COLLAR<br>
<input id="lining" class="material-value" type="text" value="" /> LINING<br>
<input id="tongue" class="material-value" type="text" value="" />TONGUE<br>
</div>
&#13;
答案 1 :(得分:0)
You could add another attribute: data-segment
mtr += '<div class="material-btn" id="' + segments + '_' + material + '" data-segment="' + segments + '">' + material + '</div>';
and then you can use
var value = $(this).attr('id');
var result = $($(this).data('segment')).attr('value', value);
to select the correct input-field
答案 2 :(得分:0)
仅用于演示目的,将输入类型保留为文本。
var a = document.querySelectorAll('.material-btn');
for(i=0;i<a.length;i++){
a[i].onclick = function(){
var type= this.getAttribute('target');
document.getElementById(type).value = this.innerHTML
}
}
&#13;
<div class="sole_panel container active">
<h3>sole</h3>
<div id="sole_leather" class="material-btn" target="sole">leather</div>
<div id="sole_suade" class="material-btn" target="sole">suade</div>
<div id="sole_nubuck" class="material-btn" target="sole">nubuck</div>
</div>
<div class="collar_panel container">
<h3>collar</h3>
<div id="collar_leather" class="material-btn" target="collar">leather</div>
<div id="collar_suade" class="material-btn" target="collar">suade</div>
<div id="collar_nubuck" class="material-btn" target="collar">nubuck</div>
</div>
<div class="sole_panel container active"></div>
<input id="sole" class="material-value" type="text" value="sole_leather">
<div class="collar_panel container"></div>
<input id="collar" class="material-value" type="text" value="sole_leather">
<div class="lining_panel container"></div>
<input id="lining" class="material-value" type="text" value="sole_leather">
<div class="tongue_panel container"></div>
<input id="tongue" class="material-value" type="text" value="sole_leather">
&#13;