模拟点击单选按钮
Simulate click on radio button
我需要模拟鼠标点击单选按钮,但该按钮仅被选中。如何让功能适用于案例?
$("input[value='0']").click();
$('input[name=level]').on('click', function(event) {
switch ($(this).val()) {
case '0':
alert("zero");
break;
case '1':
alert("one");
break;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<label>
1<input type="radio" class="option-input radio" checked value="1" name="level" />
0<input type="radio" class="option-input radio" value="0" name="level" />
</label>
将触发器放在点击功能下方的元素上。您试图触发对尚未在 DOM.
中定义的函数的点击
$('input[name=level]').on('click', function(event) {
switch ($(this).val()) {
case '0':
alert("zero");
break;
case '1':
alert("one");
break;
}
});
$("input[value=0]").trigger('click');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
1<input type="radio" class="option-input radio" checked value="1" name="level" />
0<input type="radio" class="option-input radio" value="0" name="level" />
</label>
我需要模拟鼠标点击单选按钮,但该按钮仅被选中。如何让功能适用于案例?
$("input[value='0']").click();
$('input[name=level]').on('click', function(event) {
switch ($(this).val()) {
case '0':
alert("zero");
break;
case '1':
alert("one");
break;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<label>
1<input type="radio" class="option-input radio" checked value="1" name="level" />
0<input type="radio" class="option-input radio" value="0" name="level" />
</label>
将触发器放在点击功能下方的元素上。您试图触发对尚未在 DOM.
中定义的函数的点击$('input[name=level]').on('click', function(event) {
switch ($(this).val()) {
case '0':
alert("zero");
break;
case '1':
alert("one");
break;
}
});
$("input[value=0]").trigger('click');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
1<input type="radio" class="option-input radio" checked value="1" name="level" />
0<input type="radio" class="option-input radio" value="0" name="level" />
</label>