我有3个输入框,每个输入框上方有单选按钮,用于填充框的值。每个框有5个单选按钮。每个框/按钮组应独立运行。它们是用相同的类创建的,因此我使用onclick单选按钮侦听器根据单选按钮来调整输入框中的值,就像这样
document.getElementsByClassName('nyp-input')[0].value=this.value;
document.getElementsByClassName('nyp-input')[1].value=this.value;
document.getElementsByClassName('nyp-input')[2].value=this.value;
这不起作用,因为它会调整所有3个,但我需要它仅调整最接近的输入。我已经尝试过以下
jQuery('input').closest('nyp').find('.nyp-input');
adj.value=this.value;
上下文中的完整代码段
<div class="nyp" data-price="0" data-max-price="" >
<div class="price-wrapper">
<div class="donation-options">
<label for="nyp-suggested-1">
<input type="radio" name="nyp-suggested" value="20" id="nyp-suggested-1" onclick="
var adj = jQuery('input').closest('nyp').find('.nyp-input');
adj.value=this.value;
"> $20
</label>
<label for="nyp-suggested-2">
<input type="radio" name="nyp-suggested" value="50" id="nyp-suggested-2" onclick="document.getElementsByClassName('nyp-input')[0].value=this.value; document.getElementsByClassName('nyp-input')[1].value=this.value; document.getElementsByClassName('nyp-input')[2].value=this.value;"> $50
</label>
<label for="nyp-suggested-3">
<input type="radio" name="nyp-suggested" value="100" id="nyp-suggested-3" onclick="document.getElementsByClassName('nyp-input')[0].value=this.value; document.getElementsByClassName('nyp-input')[1].value=this.value; document.getElementsByClassName('nyp-input')[2].value=this.value;"> $100
</label>
<label for="nyp-suggested-4">
<input type="radio" name="nyp-suggested" value="500" id="nyp-suggested-4" onclick="document.getElementsByClassName('nyp-input')[0].value=this.value; document.getElementsByClassName('nyp-input')[1].value=this.value; document.getElementsByClassName('nyp-input')[2].value=this.value;"> $500
</label>
</div>
</div>
<label for="nyp">
Other ( $ ) </label>
<input id="nyp" name="nyp" type="text" value="0.00" title="nyp" class="input-text amount nyp-input text">
如何获取最接近的nyp输入来调整值。
答案 0 :(得分:2)
尝试此代码
$(function(){
$('.nyp-radio').click(function(){
$(this).parents('.price-wrapper').children('[type="text"]').val($(this).val());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nyp" data-price="0" data-max-price="" >
<div class="price-wrapper">
<div class="donation-options">
<label for="nyp-suggested-1">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="20" id="nyp-suggested-1" > $20
</label>
<label for="nyp-suggested-2">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="50" id="nyp-suggested-2" > $50
</label>
<label for="nyp-suggested-3">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="100" id="nyp-suggested-3" > $100
</label>
<label for="nyp-suggested-4">
<input type="radio" class='nyp-radio' name="nyp-suggested" value="500" id="nyp-suggested-4" > $500
</label>
</div>
<label for="nyp">
Other ( $ ) </label>
<input id="nyp" name="nyp" type="text" value="0.00" title="nyp" class="input-text amount nyp-input text">
</div>
<div class="nyp" data-price="0" data-max-price="" >
<div class="price-wrapper">
<div class="donation-options">
<label for="nyp-suggested-11">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="20" id="nyp-suggested-11" > $20
</label>
<label for="nyp-suggested-12">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="50" id="nyp-suggested-12" > $50
</label>
<label for="nyp-suggested-13">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="100" id="nyp-suggested-13" > $100
</label>
<label for="nyp-suggested-14">
<input type="radio" class='nyp-radio' name="nyp-suggested1" value="500" id="nyp-suggested-14" > $500
</label>
</div>
<label for="nyp">
Other ( $ ) </label>
<input id="nyp" name="nyp" type="text" value="0.00" title="nyp" class="input-text amount nyp-input text">
</div>
答案 1 :(得分:0)
您可能遇到的问题是3个“输入框”包含具有相同ID的输入(基本上,例如,您有3次nyp-suggested-1)。您应该尝试为每个组生成不同的ID,并保持相同的名称,但各组之间应不同-示例:
group1:
<label for="nya1">
<input type="radio" name="nya" value="20" id="nya1" > $20
</label>
<label for="nya2">
<input type="radio" name="nya" value="50" id="nya2" > $50
</label>
<label for="nya3">
<input type="radio" name="nya" value="100" id="nya3" > $100
</label>
<label for="nya4">
<input type="radio" name="nya" value="500" id="nya4" > $500
</label>
第2组:
<label for="nyb1">
<input type="radio" name="nyb" value="20" id="nyb1" > $20
</label>
<label for="nyb2">
<input type="radio" name="nyb" value="50" id="nyb2" > $50
</label>
<label for="nyb3">
<input type="radio" name="nyb" value="100" id="nyb3" > $100
</label>
<label for="nyb4">
<input type="radio" name="nyb" value="500" id="nyb4" > $500
</label>
然后,您可以根据jquery中的组名进行选择,并使用相同的值进行更新。