html - 为什么 HTML 表单中的某些标记会获得名称属性,而其他标记会获得值属性?

标签 html attributes name-attribute

我正在学习如何编写 HTML 表单,对于我遇到的示例有几个问题。

  1. 用外行术语来说,值和名称有什么区别?

  2. 为什么前两个标签的输入中的值保留为空字符串?

  3. 为什么第三个标签的 select 只给出了名称而不给出值,而后面​​ 3 个选项只给出了值?

  4. 第5个标签输入的value后面有一个S属性,是什么意思?

  5. 为什么第 6 个标签的输入同时指定了名称和值?

  6. 为什么第6个输入的名称是“facilities[]”,这和数组有什么关系吗?

  7. 再说一遍,为什么第 8 个标签的输入有名称和值,但它的按钮有类和名称?

我确实需要针对每个标签的具体答案,因为我仍然是网络开发的初学者。

这是我的代码:

<form id="registration_form" method="POST">


  <!-- 1st-->
  <label>Full name*:</label>
  <br>
  <input type="text" name="full_name" placeholder="FirstName LastName" autofocus="autofocus" value="">
  <br>


  <!-- 2nd-->
  <label>Email address*:</label>
  <br>
  <!-- value leaves input box empty-->
  <input type="text" name="email_addr" value="" placeholder="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="274d464a42545552545467425f464a574b420944484a" rel="noreferrer noopener nofollow">[email protected]</a>">
  <br>



  <!-- 3rd-->
  <label>Select Tour Package* :</label>
  <br>
  <select name="package">
    <option value="Goa">Goa</option>
    <option value="Kashmir">Kashmir</option>
    <option value="Rajasthan">Rajasthan</option>
  </select>
  <br>

  <!-- 4th-->
  <label>Arrival date*:</label>
  <br>
  <input type="text" name="arv_dt" placeholder="m/d/y" value="">
  <br>


  <!--5th -->
  <label>Number of persons*:</label>
  <br>
  <input type="text" name="persons" value="" s>
  <br>



  <!-- 6th-->
  <label>What would you want to avail?*</label>
  <br>Boarding
  <input type="checkbox" name="facilities[]" value="boarding">
  <br>Fooding
  <input type="checkbox" name="facilities[]" value="fooding">
  <br>Sight seeing
  <input type="checkbox" name="facilities[]" value="sightseeing">
  <br>



  <!-- 7th-->
  <label>Discout Coupon code:</label>
  <br>
  <input type="text" name="dis_code" value="">
  <br>



  <!-- 8th-->
  <label>Terms and conditions*</label>
  <br>
  <input type="radio" name="tnc" value="agree" checked>I agree
  <input type="radio" name="tnc" value="disagree">I disagree
  <br>
  <button type="submit" class="btn btn-large btn-primary" name="submit">Complete reservation</button>



</form>

最佳答案

name 属性是元素的名称。例如,服务器可以使用它来识别表单提交中的字段。在像facilities[]这样的情况下,服务器收到时会将具有该名称的所有输入值放入一个数组中。

value 属性是默认值,将在页面加载时显示在元素中。该值也将发布到服务器并关联回名称。

类通常仅用于 CSS 属性或 JavaScript,与您问题中的其他任何内容都没有真正相关。

对于您的情况中的 S 属性,它可能没有任何意义,只是一个拼写错误。如果您使用类似 Angular's directives 的内容,它可能具有一定的意义。

关于html - 为什么 HTML 表单中的某些标记会获得名称属性,而其他标记会获得值属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36506059/

相关文章:

javascript - 调用canvas.toDataURL时,为什么rasterizeHTML.drawHTML部分是黑色的?

jquery - 逐个添加类,每个类一秒后添加

jquery ui ressized() 不工作

c++ - 我无法使用 C++ 访问类的属性

python - 在Python中通过多个类传递属性

html - 如何修复位置和大小错误的 SVG 字形?

c# - 我可以安全地在文件夹上使用 `IO.File.GetAttributes` 吗?

Javascript 只触发一次

jquery - 使用jQuery验证器插件,如何使用动态创建的名称属性来分配规则?