【问题标题】:How to change the value of input type in html?如何更改html中输入类型的值?
【发布时间】:2017-12-28 22:06:27
【问题描述】:

我有一个简单的 html 表单,即

  <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top" id="registration-form">
     <input type="text" class="form-control" name="form-reg-fullname" data-form-field="Name" id="form-reg-fullname">
     <input type="hidden" name="return" value=""/>
  </form>

&lt;script&gt;标签中我正在做验证并改变return的值:

 <script>
   $('#registration-form').submit(function () {
   var fname = $.trim($('#form-reg-fullname').val());

$("#return").val("http://localhost:9000/app/fname");

});
</script>

提交表单时验证成功,但我在return input type 中得到空值。我怎样才能让它工作?

【问题讨论】:

    标签: javascript jquery html html-input


    【解决方案1】:

    看起来您在返回输入中缺少一个 id,您只有一个名称。所以它应该是这样的:

    <input type="hidden" name="return" id="return" value=""/>
    

    这样,当您在 js 中使用 $('#return') 调用输入时,它会通过它的正确 id 获取它。

    【讨论】:

      【解决方案2】:
        <input type="hidden" name="return" value=""/>
      
      $("#return").val("http://localhost:9000/app/fname");//PROBLEM RESIDE Here
      

      看看你在做什么,你正在尝试将值分配给具有 id = return 的选择器。但是您没有任何具有此 id 的元素,但是您有一个具有相同名称的元素。您可以通过两种方式解决此问题:

      首先按名称选择元素:

      $("input[name='return']").val("http://localhost:9000/app/fname");
      

      或者您可以将 id 属性分配给您的输入元素

      <input type="hidden" id="return" name="return" value=""/>
      

      【讨论】:

        【解决方案3】:

        您正在使用不存在的id="return" 分配输入值。

        $("#return").val("http://localhost:9000/app/fname");
        

        你应该给输入标签一个id

        <input type="hidden" name="return" id="return" value=""/>
        

        或者把脚本改成

        $("input[name='return']").val("http://localhost:9000/app/fname");
        

        【讨论】:

          【解决方案4】:

          您在 jquery 中使用 id 调用节点,您的返回输入没有称为 return 的 Id

             $('#registration-form').submit(function () {
             var fname = $.trim($('#form-reg-fullname').val());
          
          $("#return").val("http://localhost:9000/app/fname"); //setting the value
          console.log($("#return").val()); //getting the value
          });
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top" id="registration-form">
               <input type="text" class="form-control" name="form-reg-fullname" data-form-field="Name" id="form-reg-fullname">
               <input type="hidden" name="return" value="" id="return"/>
            </form>

          【讨论】:

            猜你喜欢
            • 2012-02-24
            • 1970-01-01
            • 2020-06-16
            • 2021-05-04
            • 2014-05-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-15
            相关资源
            最近更新 更多