【问题标题】:Trouble storing form field value as jQuery variable无法将表单字段值存储为 jQuery 变量
【发布时间】:2016-06-29 18:57:07
【问题描述】:

我在将表单字段值存储为 jQuery 变量时遇到问题。我的控制文本像魅力一样存储和回忆。经过多次测试,我仍然不确定我在哪里错过了这个。也许是我没看到的愚蠢的人类把戏?

这里是小提琴:https://jsfiddle.net/brokenmold/f78vrs16/5/

<form class="ajaxform form-horizontal" method="POST" action="">
    <label for="billing_org" class="sr-only">Billing Org</label><br>
    <input type="text" name="billing_org" class="form-control" value="Test Billing Co" placeholder="Billing Org"><br><br>

    <label>
        <input type="checkbox" name="shipping_same" id="shipwreck"> Ship Same As Billing
    </label><br><br>

    <label for="shipping_org" class="sr-only">Shipping Org</label><br>
    <input type="text" name="shipping_org" id="shipping_org" class="form-control" value="Not Checked" placeholder="Shipping Org"><br><br>

    <label for="control_test" class="sr-only">Control Test</label><br>
    <input type="text" name="control_test" id="control_test" class="form-control" value="Not Checked" placeholder="Contol Test">
</form>
$("#shipwreck").on('click', function() {
    var billing_org = $('#billing_org').val();
    var control_test = "Control Test";

    if ($('#shipwreck').prop('checked')) {
        $("#shipping_org").val(billing_org);
        $("#control_test").val(control_test);
    } else {
        $("#shipping_org").val("Not Checked");
        $("#control_test").val("Not Checked");
    }
});

https://jsfiddle.net/brokenmold/f78vrs16/5/

【问题讨论】:

  • 您对此代码有什么具体问题吗?看来这里工作正常。
  • 你的意思是勾选和取消勾选后值没有变化?
  • jsfiddle 工作正常
  • 检查时,帐单价值未显示为应有的运费价值。
  • 那是因为你没有 idbilling_org 的元素

标签: javascript jquery forms var


【解决方案1】:

您的 HTML 存在缺陷。 billing_org 字段上没有 ID,因此您存储该值的代码将不起作用。将输入字段的 HTML 更改为此,您应该没问题:

<input type="text" name="billing_org" id="billing_org" class="form-control" value="Test Billing Co" placeholder="Billing Org">

上面与原始代码的唯一变化是添加了id="billing_org"

总是简单的漏掉!

【讨论】:

  • 您说的太对了,我在 1 个字段上进行了测试,但我错过了将 ID 放在上面.. 显然没有抓住它。 :P
猜你喜欢
  • 2020-04-18
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多