javascript选择框值不更新
我希望你能帮忙 我有以下HTML代码:javascript选择框值不更新,javascript,html,Javascript,Html,我希望你能帮忙 我有以下HTML代码: <select id='bonus1frequency'> <option value="0">Annual</option> <option value="1">Half Yearly</option> <option value="2">Quarterly</option> </select> 编辑: 我在主HTML文档中使用以下JQUERY来切换用户选择
<select id='bonus1frequency'>
<option value="0">Annual</option>
<option value="1">Half Yearly</option>
<option value="2">Quarterly</option>
</select>
编辑:
我在主HTML文档中使用以下JQUERY来切换用户选择每个选项时显示的字段。因此,如果用户在选择框中选择选项值0“年度”,以下代码将显示包含年度奖金字段的年度奖金分割器,并隐藏显示半年和季度奖金字段的分割器
基于关于我需要如何将重置这些字段与更改事件联系起来的评论,我只是想也许我可以在下面的jquery代码中将字段重置为零。然而,我对JQUERy甚至不太熟悉,也不知道如何做到这一点……任何帮助都将不胜感激
$('#bonus1frequency').on('change', function() {
if ( this.value == '0')
//.....................^.......
{
$("#annual").show();
$("#halfyearly").hide();
$("#quarterly").hide();
}
if ( this.value == '1')
//.....................^.......
{
$("#annual").hide();
$("#halfyearly").show();
$("#quarterly").hide();
}
编辑2
我已将代码更新如下:
$('#bonus1frequency').on('change', function() {
if ( this.value == '1')
//.....................^.......
{
$("#annual").show();
$("#halfyearly").hide();
$("#quarterly").hide();
$("#halfyear_bonus_one").val("0");
这会阻止我在“半年奖金一”字段中输入值。它只是不断地将值重置为零。我在“选择”框中添加了一个空的“选择”选项,但没有任何区别。有什么想法吗?代码的问题是没有将其绑定到select的更改事件。它将在加载页面时运行,由于下拉列表中的默认选项为“年度”,因此显示为“半年奖金”值的值将为0.00,并且不会更改,即使用户将在下拉列表中选择其他选项 您应该创建一个js函数,其中包含您的代码,并且几乎没有修改 然后将其绑定到下拉列表的更改事件。 此外,您应该添加条件来处理其他场景:半年一次和每季度一次
var bonusval1 = document.getElementById("bonus1frequency");
bonusval1.onchange = function () {
var strUser = bonusval1.options[bonusval1.selectedIndex].value;
if (strUser == "0"){ //if annual bonus is selected
document.getElementById("halfyear_bonus_one").value = "0.00";
} else if(strUser == 1){ //Half yearly is selcted
document.getElementById("halfyear_bonus_one").value = "1.00";
} else { //quertly is selected
document.getElementById("halfyear_bonus_one").value = "2.00";
}
}
现在,还有另一个问题-默认值是第一个值-但onchange事件不会触发。我们可以在onchange事件之外运行函数来处理默认值,或者您可以在下拉列表中添加一个空白选项作为第一个选项,例如Choose:或其他任何选项
关于第二个问题
我不明白为什么你会突然使用jQuery,而你已经这么做了
写了很好的js代码。这类决定应在进程开始时加以考虑
无论如何,您可以使用.val函数来更改输入字段的值。我没有你表格的代码和分隔符,但应该大致如下:
if ( this.value == '0')
//.....................^.......
{
$("#annual").show();
$("#halfyearly").hide();
$("#quarterly").hide();
$("#id_of_input_for_halfyearly").val("0");
$("#id_of_input_for_quarterly").val("0");
}
请参阅以下帮助并为季度添加代码。使用jquery而不是使用javascript代码来获取元素
<select id="bonus1frequency">
<option value="0">Annual</option>
<option value="1">Half Yearly</option>
<option value="2">Quarterly</option>
</select>
<div id="annual" style="display: none;">
<input type="text" id="a1">
</div>
<div id="half" style="display: block;">
<input type="text" class="b2">
<input type="text" class="b2">
<script>
$('#half').hide()//Same as this hide quarterly div
$("#bonus1frequency").change(function(){
var strUser = $(this).val()
if (strUser === "0") //if annual bonus is selected
{
$('#annual').show()
$('#half').hide()
$(".b2").val(0);
}else{
$('#half').show()
$('#annual').hide()
$('#a1').val(0)
}
})
</script>
</div>
我想这会帮你的
jQuery('#bonus1frequency').on('change',function(){
if(this.value==1){
jQuery('.yearly').val('').show();
jQuery('.half-yearly').val(0).hide();
jQuery('.quarterly').val(0).hide();
}else if(this.value==2){
jQuery('.yearly').val(0).hide();
jQuery('.half-yearly').val('').show();
jQuery('.quarterly').val(0).hide();
}else if(this.value==3){
jQuery('.yearly').val(0).hide();
jQuery('.half-yearly').val(0).hide();
jQuery('.quarterly').val('').show();
}
})
选中此项0.00;只是将为0,而不是进行比较。strUser=0您确定您的if语句吗?如果strUser==0,我想应该是。谢谢,这让我可以在半年奖金字段中输入一个值,但是当我使用html select框码切换回年度奖金时,该值仍然保留,部分是清晰的。您是否在jquery事件“change”中编写了此代码?目前,我还有以下jquery,它允许我根据用户在下拉框中选择的内容显示/隐藏字段:Hi Ofir,我是否可以向jquery代码中添加任何内容来重置jquery“$”bonus1frequency“中的值。关于“change”,函数{如果this.value=='0'/....^……{$annual.show;$halfyear.hide;$quarterly.hide;}“你是在使用jQuery还是JS?你能编辑你的主要问题并添加你的最后一个问题吗?你在jQuery中说重置值是什么意思?我在回答中提到了你的第二个问题。嗨,谢谢,我已经尝试过了,但仍然不起作用-我会再次编辑我的主要答案。老实说,很难理解你想要什么。你能不能也许可以添加一个示例?您可以在季度中使用相同的示例。感谢大家的帮助。最后,使用上面的答案进行操作。非常感谢:o
jQuery('#bonus1frequency').on('change',function(){
if(this.value==1){
jQuery('.yearly').val('').show();
jQuery('.half-yearly').val(0).hide();
jQuery('.quarterly').val(0).hide();
}else if(this.value==2){
jQuery('.yearly').val(0).hide();
jQuery('.half-yearly').val('').show();
jQuery('.quarterly').val(0).hide();
}else if(this.value==3){
jQuery('.yearly').val(0).hide();
jQuery('.half-yearly').val(0).hide();
jQuery('.quarterly').val('').show();
}
})