【发布时间】:2019-06-03 19:59:02
【问题描述】:
您好,我正在尝试根据选择的单选按钮添加数据并删除数据,但在我的情况下,我选择的任何按钮都将附加到 div 中,您能否解决如何添加的问题并根据选中的单选按钮删除数据。
$( document ).ready(function() {
$(".paymethod").on("click", function(){
paymentMethod = $(this).val();
switch(paymentMethod){
case "stripe":
$(".stripe_fields").append("stripe appended text.");
break;
case "square":
$(".square_fields").append("Square appended text.");
break;
case "authorize.net":
$(".authorize.net_fields").append("Authorize appended text.");
break;
case "paypal":
$(".paypal_fields").append("Paypal appended text.");
break;
default:
alert("select a payment method");
}
});
});
label{
display: inline-block;
}
label:hover{
cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<label>
<input type="radio" class="paymethod" name="radio" value="square">square
<div class="square_fields"></div>
</label>
<label>
<input type="radio" class="paymethod" name="radio" value="stripe">stripe
<div class="stripe_fields"></div>
</label>
<label>
<input type="radio" class="paymethod" name="radio" value="authorize.net">authorize.net
<div class="authorize.net_fields"></div>
</label>
<label>
<input type="radio" class="paymethod" name="radio" value="paypal">paypal
<div class="paypal_fields"></div>
</label>
</form>
【问题讨论】:
-
使用更改事件而不是点击
-
@BearNithi 我改了但没用
-
删除图标 id $('#your id').click(function() { $(this).remove(); });
-
@Ragupathi 我没听懂你在说什么
-
您的意思是,如果您之前选择了
square,而现在您选择了stripe,您想将Square appended text替换为stripe appended text???
标签: javascript jquery html switch-statement