[英]using select and option javascript/jquery getting the wrong data-value using (this).data(“value”)
I have this code that send that data value to my server 我有这段代码将该数据值发送到我的服务器
$("#choiceamountmonthly").change(function () {
var monthamountblock = $(this).data("value");
alert("maandelijks pakket gekozen");
$.getJSON(
"server/ajax.php",
{request:"addMonthsToCart", MonthsToCart: monthamountblock},
function (data) {
switch(data.response){
case "success":
alert("Met succes toegevoegd");
break;
}
}
);
});
Here it gets the data-value. 在这里,它获取数据值。
<select id="choiceamountmonthly" data-value="1000" name="subscription_choice" title="subscription_choice">
<option data-value="0" >-Kies het aantal maanden-</option>
<option data-value="3">3 maanden</option>
<option data-value="5">6 maanden</option>
<option data-value="9">12 maanden</option>
</select>
In my code here it takes the data-value
of the select so it sends 1000 to my server. 在我的代码中,它采用了select的
data-value
,因此将1000发送到我的服务器。 But I want to send the data-value
of the option to my server. 但是我想将选项的
data-value
发送到我的服务器。 How can I do this? 我怎样才能做到这一点?
Instead of: 代替:
var monthamountblock = $(this).data("value");
What you need is: 您需要的是:
var monthamountblock = $(':selected', this).data("value");
This grabs the selected option's data from the select, rather than the select's data itself. 这将从选择中获取选择的选项的数据,而不是选择的数据本身。
Hope this helps! 希望这可以帮助!
you can also use only value (instead of data-value ) for select options: 您还可以仅将值 (而不是data-value )用于选择选项:
<select id="choiceamountmonthly"
data-value="1000"
name="subscription_choice"
title="subscription_choice">
<option value="0" >-Kies het aantal maanden-</option>
<option value="3">3 maanden</option>
<option value="5">6 maanden</option>
<option value="9">12 maanden</option>
</select>
than you can grab it like this: 比您可以像这样抓住它:
var monthamountblock = $(this).val();
plunker: http://plnkr.co/edit/jtMdP3E2LJMBs6CzBzLQ?p=preview 塞子: http ://plnkr.co/edit/jtMdP3E2LJMBs6CzBzLQ?p=preview
from what i can see, you dont need the data-value on the select. 从我所看到的,您不需要选择的数据值。 This seems to be overriding the data-value of the options.
这似乎覆盖了选项的数据值。 The select should just need the name, no value.
选择应该只需要名称,没有值。
IE: IE浏览器:
`<select id="choiceamountmonthly" name="subscription_choice" title="subscription_choice">
<option data-value="0" >-Kies het aantal maanden-</option>
<option data-value="3">3 maanden</option>
<option data-value="5">6 maanden</option>
<option data-value="9">12 maanden</option>
</select>
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.