从选定的optgroup获取值并将其存储在Jquery变量中

时间:2014-06-03 14:52:56

标签: php jquery html

这就是我想要实现的目标。我有3个optgroups选择,3个选择具有相同的标签名称=" DataEntered"我需要将选定的选项传递给Jquery变量以提交该值而不刷新表单。几乎一切都很好,我只有2个问题。

1-此Jquery函数仅在第一个选择optgroup中的一个选项被选中时才取值,对剩余的2个选项不起作用,即使它们具有相同的标记名称

2-一旦传递了第一个选定值,我需要重置该选择,因为在第二个提交中仍然传递了所选第一个选项的值。

Jquery功能:

DataEntered:$('select[name=DataEntered]').val()

Html选择:

<select  class="DropDown" Name="DataEntered">
<optgroup label="Customer Adjustment">
<option value="Agent called to make a Turn on/off">Turn on/off</option>
<option value="Agent called to make a credit Change">Credit Change</option>
<option value="Agent called to make a Temp Cred Change ">Temp Cred Change</option>
<option value="Agent called to Open New Customer">Open New Customer</option>
<option value="Agent called for other reason">Other</option>
</optgroup>

<optgroup label="Product Question">
<option value="Agent called for Question1">Question1</option>
<option value="Agent called  for Question2">Question2</option>
<option value="Agent called for Question3">Question3</option>
<option value="Agent called for Question4">Question4</option>
<option value="Agent called for Other Question">Other</option>
</optgroup>
</select>


<select  class="DropDown"  Name="DataEntered">
<optgroup label="Account Adjustment">
<option value="Acc Adj-Change PW">Change PW</option>
<option value="Acc Adj-More Credit">More Credit</option>
<option value="Acc Adj-Turn off account">Turn off account</option>
</optgroup>

<optgroup label="Product Question">
<option value="Agent called for Question1">Question1</option>
<option value="Agent called  for Question2">Question2</option>
<option value="Agent called for Question3">Question3</option>
<option value="Agent called for Question4">Question4</option>
<option value="Agent called for Other Question">Other</option>
</optgroup>
</select>


<select  class="DropDown"  Name="DataEntered">
<option value="Runner-Check item it">Check item it</option>
<option value="Runner-Check item it">Receive Work</option>
<option value="Runner-Check item it">Confirm/Ask for information</option>
<option value="Runner-Check item it">Problem</option>
<option value="Runner-Check item it">Other</option>
</select>

4 个答案:

答案 0 :(得分:0)

您应该使用不同的名称(DataEntered1,DataEntered2,DataEntered3)或通过id示例选择它们:

DataEntered:$('#dataenterd1').val()
<select id='dataenterd1' class="DropDown"  Name="DataEntered">

DataEntered:$('#dataenterd1').val() <select id='dataenterd1' class="DropDown" Name="DataEntered">

答案 1 :(得分:0)

通过给出相同的名称,DataEntered现在变成了一个数组。您可以像这样访问每个选择的值:

DataEntered:$('select[name=DataEntered]');
Selected1: $(DataEntered[0]).val();
Selected2: $(DataEntered[1]).val();
Selected3: $(DataEntered[2]).val();
然而,这不是一个好的设计。您应该为每个SELECT提供自己的名称或类或ID。

答案 2 :(得分:0)

更简单的方式,可以根据需要进行选择:

<script type="text/javascript">
    function getSelectValues(){
        $('select[name="DataEntered"]').each(function(){
            console.log($(this).val()); // here you can manage the value however you want to
        });
    };
</script>

答案 3 :(得分:0)

NULL