仅在单击时获取选择值

时间:2021-10-07 21:21:18

I have a select input with a few options, and a jQuery code who show a div when you select a certain option. At moment i'm using this :

我有一个带有几个选项的选择输入,以及一个在选择某个选项时显示div的jQuery代码。我正在使用这个:

$('#id_treatment_type').val() == '1'

It work great on chrome, but not in firefox. When I set the mouse on the option (whitout clicking) it change the value of the option.

它适用于chrome,但不适用于Firefox。当我在选项上设置鼠标(白色点击)时,它会更改选项的值。

On Chrome it work because the value change only when I clicked on the option.

在Chrome上它可以正常工作,因为只有在我点击该选项时值才会更改。

The problem is that I have to put this in a loop because I have way to many fields to set a .change on everyone

问题是我必须将它放在循环中,因为我可以通过许多字段为每个人设置.change

window.setInterval(function(){alerts();}, 5000);

I would set this interval to a few ms because I need to show the div faster.

我会将此间隔设置为几毫秒,因为我需要更快地显示div。

I need to have it work on firefox but I don't know how if someone have an idea. Sorry for my english, but I hope you could understand what i'm trying to say.

我需要让它在Firefox上工作,但我不知道如果有人有想法。对不起我的英语,但我希望你能理解我想说的话。

Thank you

2 个解决方案

#1


You can use the change event. Example

您可以使用更改事件。例

    $('#id_treatment_type').on('change', function(){
		alert($(this).val());
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="id_treatment_type" id="id_treatment_type">
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
</select>

#2


I made a fiddle with a small example:

我用一个小例子做了一个小提琴:

https://jsfiddle.net/usz5pyhv/

<select id="mySelect">  
    <option value="1">one</option>
    <option value="2">two</option>
</select>
<div id="whenValOne">Value is one</div>

$(function() {
    $("#mySelect").change("change", function() {
       if(this.value == 1)
         $("#whenValOne").show();
       else  
         $("#whenValOne").hide();            
    });
});

#1


You can use the change event. Example

您可以使用更改事件。例

    $('#id_treatment_type').on('change', function(){
		alert($(this).val());
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="id_treatment_type" id="id_treatment_type">
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
</select>

#2


I made a fiddle with a small example:

我用一个小例子做了一个小提琴:

https://jsfiddle.net/usz5pyhv/

<select id="mySelect">  
    <option value="1">one</option>
    <option value="2">two</option>
</select>
<div id="whenValOne">Value is one</div>

$(function() {
    $("#mySelect").change("change", function() {
       if(this.value == 1)
         $("#whenValOne").show();
       else  
         $("#whenValOne").hide();            
    });
});