我有3个单选按钮。我想在每次单击第一个单选按钮(即sel_address)时刷新我的页面。每当用户点击所选地址时,页面应该刷新。我不想在点击其他2个单选按钮时刷新页面。我怎么能实现这个目标?
<label class="option sel_address">
<input type="radio" value="sel_address" name="delivery_option" checked="checked" id="sel_address">
</label>
<strong>selected address</strong>
<label class="option your_school">
<input type="radio" value="your_school" name="delivery_option" id="your_school">
</label>
<strong>school</strong>
<label class="option rhs_showroom">
<input type="radio" value="showroom" name="delivery_option" id="showroom">
</label>
<strong>Showroom</strong>
<script type="text/javascript">
$("input[name='delivery_option']").click(function() {
.
.
var delivery_value = $(this).val();
if(delivery_value == "your_school" || delivery_value == "showroom"){
//some code
}else{
..
}
}
答案 0 :(得分:2)
在“body”标记结束之前包含以下脚本。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$("#sel_address").click(function(){
window.location = "";
});
</script>
答案 1 :(得分:1)
您可以使用eq(0)
仅选择第一个元素:
$("input[name='delivery_option']").eq(0).click(function() {
console.log('First was clicked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="option sel_address">
<input type="radio" value="sel_address" name="delivery_option" id="sel_address">
</label>
<strong>selected address</strong>
<label class="option your_school">
<input type="radio" value="your_school" name="delivery_option" id="your_school">
</label>
<strong>school</strong>
<label class="option rhs_showroom">
<input type="radio" value="showroom" name="delivery_option" id="showroom">
</label>
<strong>Showroom</strong>
或:first
pseudo class selector:
$("input[name='delivery_option']:first").click(function() {
console.log('First was clicked');
});
答案 2 :(得分:0)
建议BeNdErR和Dekel,使用:first
pseudo-selector仅选择第一个单选按钮。
关于重新加载页面,请按照location.reload()
的建议使用this question。
$("input[type='radio']:first").click(function() {
location.reload();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="option sel_address">
<input type="radio" value="sel_address" name="delivery_option" checked="checked" id="sel_address">
</label>
<strong>selected address</strong>
<label class="option your_school">
<input type="radio" value="your_school" name="delivery_option" id="your_school">
</label>
<strong>school</strong>
<label class="option rhs_showroom">
<input type="radio" value="showroom" name="delivery_option" id="showroom">
</label>
<strong>Showroom</strong>