我正在学习如何编写 HTML 表单,对于我遇到的示例有几个问题。
用外行术语来说,值和名称有什么区别?
为什么前两个标签的输入中的值保留为空字符串?
为什么第三个标签的 select 只给出了名称而不给出值,而后面 3 个选项只给出了值?
第5个标签输入的value后面有一个S属性,是什么意思?
为什么第 6 个标签的输入同时指定了名称和值?
为什么第6个输入的名称是“facilities[]”,这和数组有什么关系吗?
再说一遍,为什么第 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/