如何更改html中输入类型的值?

How to change the value of input type in html?

我有一个简单的 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>

<script> 标签中我正在进行验证并更改 return:

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

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

});
</script>

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

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

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

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

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

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

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

首先按名称选择元素:

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

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

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

您分配的输入值 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");

您使用 jquery 中的 ID 调用节点,您的 return 输入没有名为 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>