我有一個網絡表單,我想根據相應的單選按鈕選擇更改文本輸入的值。我不想使用每個文本輸入和單選按鈕的ID,我想遵循一個簡單的模式,其中文本輸入基於共享(非直接)父字段集的單選按鈕上的值。更改基於收音機與同一父母的輸入值
有多種方法可以實現這一點,但我的策略是查找單選按鈕的最近的父級字段集(也是文本輸入的父級字段集),搜索正確的文本輸入並更改文本輸入的值。
這裏是我到目前爲止,但它不工作:
jQuery('.big-container input[id$="-selector-1"]').change(function() {
var parent = jQuery('.smaller-container input[id$="-selector-1"]').closest('fieldset');
jQuery('parent').find('.fieldset-wrapper div:nth-child(2) input').val('1');
});
jQuery('.big-container input[id$="-selector-2"]').change(function() {
var parent = jQuery('.smaller-container input[id$="-selector-2"]').closest('fieldset');
jQuery('parent').find('.fieldset-wrapper div:nth-child(2) input').val('0');
});
HTML:
<fieldset class="big-wrapper">
<div class="fieldset-wrapper">
<fieldset class="medium-wrapper-1">
<div class="fieldset-wrapper">
<div>...</div> <!-- unimportant -->
<fieldset class="smaller-container-1">
<div class="fieldset-wrapper">
<div>
<label>...</label>
<div class="form-radios">
<div class="radio-1-wrapper">
<div class="input-label-wrapper">
<input id="blahblahblah-selector-1">...</input>
<label>...</label>
</div>
</div>
<div class="radio-2-wrapper">
<div class="input-label-wrapper">
<input id="blahblahblah-selector-2">...</input>
<label>...</label>
</div>
</div>
</div>
</div>
<div class="text-input-X">
<label>...</label>
<input>...</input>
</div>
<div class="text-input-Y">...</div> <!-- parallel to .text-input-X -->
<div class="text-input-Z">...</div> <!-- parallel to .text-input-X -->
</div>
</fieldset>
<fieldset class="smaller-container-2">...</fieldset> <!-- parallel to .smaller-container-1 -->
<fieldset class="smaller-container-3">...</fieldset> <!-- parallel to .smaller-container-1 -->
<fieldset class="smaller-container-4">...</fieldset> <!-- parallel to .smaller-container-1 -->
</div>
</fieldset>
<fieldset class="medium-wrapper-2">...</fieldset> <!-- parallel to .medium-wrapper-1 -->
</div>
</fieldset>
如果它是不明確的,input[id$="-selector-1"]
和input[id$="-selector-2"]
是單選按鈕,並div.text-input-X
內的常規輸入是我想要更改的那個。
我在做什麼錯?
你能發佈你的HTML標記嗎? – technophobia
縮寫HTML現在在上面。認爲看到整個事情可能會讓人分心,但要把自己擊倒。 – cjl750
您能否澄清一下:在所有字段集中,您只希望在單選按鈕更改時更新第一個文本輸入字段?第一個文本輸入的唯一值是'0'還是'1'(基於所選的單選按鈕)? – technophobia