javascript - 如何在数字输入中添加加号和减号并更新值

标签 javascript html validation input numbers

我正在为一个旅游网站构建一个表单,其中有一个部分显示“添加参与者”。我使用了数字输入,因此当它设置为“1”时,它会添加一组字段,并重复直到最多 5 个。

我现在附加了一个加号和减号按钮来更改值,使表单看起来更有吸引力,但尽管值正在更改,但它并未注册以显示隐藏字段。

这是我使用附加按钮更改值的代码:

    $(".test-fill").append('<div class="inc button">+</div><div class="dec button">-</div>');

    $(".button").on("click", function() {

    var $button = $(this);
    var oldValue = $button.parent().find(".test").val();

    if ($button.text() == "+") {
      var newVal = parseFloat(oldValue) + 1;
    } else {
   // Don't allow decrementing below zero

        if (oldValue > 0) {
          var newVal = parseFloat(oldValue) - 1;
        } else {
          newVal = 0;
        }
       }

       $button.parent().find(".test").val(newVal);

       });

这是我的代码,用于根据值显示隐藏字段:

    //Hide the field initially
$("#hide1, #hide2").hide();

//Show the text field only when the third option is chosen - this doesn't
$('#awesome').change(function() {
    if ($("#awesome").val() == "1") {
        $("#hide1").slideDown();
        $("#hide2, #hide3, #hide4, #hide5").slideUp();
    }
    else if ($("#awesome").val() == "2") {
        $("#hide2").slideDown();
        $("#hide3, #hide4, #hide5").slideUp();
    }
    else if ($("#awesome").val() == "3") {
        $("#hide3").slideDown();
        $("#hide4, #hide5").slideUp();
    }
    else if ($("#awesome").val() == "4") {
        $("#hide4").slideDown();
        $("#hide5").slideUp();
    }
    else if ($("#awesome").val() == "5") {
        $("#hide5").slideDown();
    }
    else {
        $("#hide1, #hide2, #hide3, #hide4, #hide5").slideUp();
    }
});

这是我的 HTML:

<p>Add participant<br />
<div class="test-fill">
[number number-658 min:0 max:5 id:awesome class:test]
</div>

<div id="hide1">
<h4>Second Participants Information</h4>
<p>Full Name<br />
[text text-995]</p>

<p>Sex<br />
[text text-500]</p>

<p>Age<br />
[text text-379] </p>

<p>Passport Country of Issue<br />
[text text-591] </p>
</div>

<div id="hide2">
<h4>Third Participants Information</h4>
<p>Full Name<br />
[text text-995]</p>

<p>Sex<br />
[text text-500]</p>

<p>Age<br />
[text text-379] </p>

<p>Passport Country of Issue<br />
[text text-591] </p>
</div>

<div id="hide3">
<h4>Fourth Participants Information</h4>
<p>Full Name<br />
[text text-995]</p>

<p>Sex<br />
[text text-500]</p>

<p>Age<br />
[text text-379] </p>

<p>Passport Country of Issue<br />
[text text-591] </p>
</div>

<div id="hide4">
<h4>Fifth Participants Information</h4>
<p>Full Name<br />
[text text-995]</p>

<p>Sex<br />
[text text-500]</p>

<p>Age<br />
[text text-379] </p>

<p>Passport Country of Issue<br />
[text text-591] </p>
</div>

<div id="hide5">
<h4>Sixth Participants Information</h4>
<p>Full Name<br />
[text text-995]</p>

<p>Sex<br />
[text text-500]</p>

<p>Age<br />
[text text-379] </p>

<p>Passport Country of Issue<br />
[text text-591] </p>
</div>

(我正在使用 WordPress 联系表 7)

最佳答案

如果我理解正确的话,你的问题是这样的: 单击按钮不会更改选择字段,因此不会执行您的功能。

解决此问题的最简单方法是将代码放置在更改事件中 $('#awesome').change(function() {} ...

在函数 (changeTheForm()) 内,然后在调整选择输入的值后调用此函数。

$button.parent().find("#awesome").val(newVal);
// you want your code to run now..
changeTheForm();

您不需要依赖于检测选择更改/选择输入,事实上,在我看来,删除它并稍微修改您的脚本会更好。

不过,在读取未选择任何选项的选择字段的值时,您可能会遇到 NaN 错误。您应该很容易找到解决此问题的方法。

关于javascript - 如何在数字输入中添加加号和减号并更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488708/

相关文章:

php - Laravel 4 中的自定义验证

javascript - 如何使可拖动元素在恢复时不可见?

javascript - 迭代多层对象?

javascript - jScrollpane 滚动条不工作

javascript - 使高架 div 标签对劣质 div 不导电

jquery - Page_ClientValidate 未定义 (ASP.NET MVC)

javascript - 调用对象内的函数作为指令的参数

javascript - 强制在 HTML 或 JavaScript 中进行初始缩放

php - 我如何处理这个ajax数据?

javascript - 使用jquery检查表单中的所有条件