使用select和option javascript/jquery使用(this.data)(“value”)获取错误的数据值

使用select和option javascript/jquery使用(this.data)(“value”)获取错误的数据值,javascript,jquery,html,Javascript,Jquery,Html,我有一段代码将数据值发送到我的服务器 $("#choiceamountmonthly").change(function () { var monthamountblock = $(this).data("value"); alert("maandelijks pakket gekozen"); $.getJSON( "server/ajax.php", {request:"addMonthsToCart", MonthsToCart:

我有一段代码将数据值发送到我的服务器

$("#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;
            }
        }
    );
});
这里它得到数据值

<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>

在我的代码中,它接受select的数据值,因此它将1000发送到我的服务器。但是我想将选项的数据值发送到我的服务器。我怎样才能做到这一点?

从我看到的情况来看,您不需要select上的数据值。 这似乎覆盖了选项的数据值。 select应该只需要名称,不需要值

即:


从我所看到的,您不需要选择上的数据值。 这似乎覆盖了选项的数据值。 select应该只需要名称,不需要值

即:

而不是:

var monthamountblock = $(this).data("value");
您需要的是:

var monthamountblock = $(':selected', this).data("value");
这将从select中获取选定选项的数据,而不是select的数据本身

希望这有帮助

而不是:

var monthamountblock = $(this).data("value");
您需要的是:

var monthamountblock = $(':selected', this).data("value");
这将从select中获取选定选项的数据,而不是select的数据本身

希望这有帮助

对于选择选项,也可以仅使用值而不是数据值:

<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>
plunker:

您也可以对选择选项仅使用值而不是数据值:

<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>

plunker:

将引发语法错误。我想你指的是$':selected',这个。谢谢,基本上我是在找$choiceamountmonthly:selected;。我只是假设,如果您使用它,它将以相同的方式处理:selected。我已更新代码以更正此问题:仍然不正确。您没有将:selected放入字符串中。在发布代码之前,请尝试在浏览器中运行代码。这将引发语法错误。我想你指的是$':selected',这个。谢谢,基本上我是在找$choiceamountmonthly:selected;。我只是假设,如果您使用它,它将以相同的方式处理:selected。我已更新代码以更正此问题:仍然不正确。您没有将:selected放入字符串中。在发布代码之前,请尝试在浏览器中运行代码。我已经尝试过了,但是它没有向服务器发送任何值。啊,正如obisidian指出的,我错过了jquery中的一位,您需要选择“:selected”值,即“this”。:selected.datavalue;我已经尝试过了,但是它没有向服务器发送任何值。啊,正如obisidian指出的那样,我错过了jquery中的一位,您需要选择“:selected”值,即this“:selected”;我个人认为这是更好的方法,除非出于某种原因需要使用数据值…我个人认为这是更好的方法,除非出于某种原因需要使用数据值。。。