id和name属性有什么区别?它们似乎都与提供标识符的目的相同.
我想知道(特别是关于HTML表单)是否有必要或鼓励使用两者都是出于任何原因.
我正忙着在HTML 5中为学校做点什么.
所以这是我的一些代码
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="your name" required><br>
Run Code Online (Sandbox Code Playgroud)
所以我的问题实际上是:
NAME和ID有什么区别?目的?哪一个更重要?
这是我想要实现的:检索页面上的所有产品并将它们放入数组中.这是我正在使用的代码:
$page2 = curl_exec($ch);
$doc = new DOMDocument();
@$doc->loadHTML($page2);
$nodes = $doc->getElementsByTagName('title');
$noders = $doc->getElementsByClassName('productImage');
$title = $nodes->item(0)->nodeValue;
$product = $noders->item(0)->imageObject.src;
Run Code Online (Sandbox Code Playgroud)
它适用于$title but非产品.有关信息,在HTML代码中,img标记如下所示:
<img alt="" class="productImage" data-altimages="" src="xxxx">
Run Code Online (Sandbox Code Playgroud)
我一直在看这个(PHP DOMDocument如何获取元素?)但我仍然不明白如何使它工作.
PS:我收到这个错误:
调用未定义的方法
DOMDocument::getElementsByclassName()
所以我遇到了一个奇怪的问题......我想抓住一个表格的id说:
<form id="test">
<input name="id" type="hidden" value/>
</form>
Run Code Online (Sandbox Code Playgroud)
但是运行document.getElementById("test").id不会test按预期返回,而是返回输入name="id".有谁知道这里发生了什么?
这是一个重现问题的小提琴 - > http://jsfiddle.net/jascbbfu/
我知道通过阅读HTML3.01和HTML5的W3C文档,"name"属性最初作为<a>标签的属性存在,以允许人们链接到文档中的锚点.
但是,既然所有主要的浏览器供应商都允许通过"id"属性链接到文档中的任何html元素,那么"name"属性是否还有实际用途?如果是这样,我应该如何使用"名称"属性?
我一直认为指定片段标识符的标准方法是<a name="foo"></a>.
<a href="#foo">go to foo</a>
<a name="foo"></a> <!-- obsolete method, it seems -->
<p>some content under that anchor with name</p>
Run Code Online (Sandbox Code Playgroud)
但似乎这是旧的方式,而新的方式是使用id,如下所示:
<a href="#bar">go to bar</a>
<p id="bar">some content under that p with id</p>
Run Code Online (Sandbox Code Playgroud)
事实上,W3C验证器说这个元素name已经过时了<a>.那么有两种方法可以跳转到片段标识符,但其中一种已经过时了?(那是什么时候发生的?)
(有大约之间的差异的其他问题id和name,但是这一个是关于片段标识符)
假设我有一个表格可以描述一个家庭:
<input type="text" name="mother">
<input type="text" name="father">
<input type="text" name="child">
Run Code Online (Sandbox Code Playgroud)
但是我希望人们能够在表单中添加无数个孩子.所以我可以使用Javascript继续为更多的孩子添加新的输入,但我想知道我应该如何命名这些.我简单地做child1,child2,child3等?在后端我简单地循环所有的孩子?
如果所有孩子都需要一个"年龄"输入字段怎么办?难道我只是继续添加字段,如child1_age,child2_age,child3_age?
我想这会起作用,但我觉得有一个更明智的方法.有谁知道更好的方法来解决这个问题?欢迎所有提示!
public ActionResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Customerid"]);
objCustomer.CustomerCode = Request.Form["code"];
objCustomer.Amount = Convert.ToDouble(Request.Form["amount"]);
return View(objCustomer);
}
Run Code Online (Sandbox Code Playgroud)
这是我在控制器(MVC 2 aspx)中的操作:
<form action="DisplayCustomer" method="post">
Customer id:-
<input type="text" id="Customerid" /><br />
Customer Code:-
<input type="text" id="code" /><br />
Customer amount:-
<input type="text" id="amount" /><br />
<input type="submit" value="click here" />
</form>
Run Code Online (Sandbox Code Playgroud)
这是视图。当有人点击提交按钮时,它被定向到一个新页面:
<div>
<b> ID</b> <%= Model.Id %><br />
<b> Code </b><%=Model.CustomerCode %><br />
<b> Amount</b> <%=Model.Amount %><br />
</div>
Run Code Online (Sandbox Code Playgroud)
我总是在 Id 和 Amount 中得到 0,而在 CustomerCode …
首先,我为我的问题的幼稚性质道歉。我尝试在互联网上搜索解决方案,但到目前为止运气不佳。因此,我求助于 StackOverflow 的优秀人员,因为这通常可以很快解决问题。
我正在尝试为网站制作一个非常简单的登录功能。它由 HTML 表单和 php 登录脚本组成。问题是用户名(和密码)似乎没有从表单传递到登录脚本。从表单开始,它看起来像这样:
<form name="loginForm" method="post" action="loginScript.php">
<table style="position: absolute; top:55%; left:50%; transform: translate(-50%, -50%)">
<tr>
<td>Brugernavn:</td><td><input name="edUsername" type="text" id="username" autofocus></td>
</tr>
<tr>
<td>Adgangskode:</td><td><input name="edPassword" type="password" id="password"><br></td>
</tr>
<tr>
<td style ="font-size: 10px;">Glemt din adgangskode?</td>
<td style="text-align: right;"><input name="Submit" type="submit" value="Log ind"></td>
</tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
因此,我的登录脚本中需要的信息是用户名和密码数据。我尝试使用 filter_input() 获取这些内容,因为我读到这比 $_POST[] 更安全。在登录脚本中,它看起来像这样:
// Get username and password from post form
$ed_user = filter_input(INPUT_POST, 'username');
$ed_pass = filter_input(INPUT_POST, 'password');
echo $ed_user;
Run Code Online (Sandbox Code Playgroud)
“echo”部分只是一个临时行,用于检查是否确实检索到了用户名。到目前为止,情况并非如此。所以我的问题是,我到底做错了什么?
我应该name仅将属性用于表单控件吗?今天在html5上弃用了吗?
我注意到类似的问题已经过时,所以我有些困惑。
我可以为两个jsps使用一个请求映射吗?
我目前正在从一个控制器调用一个请求映射,但其中一个jsps似乎没有被控制器捕获.
两个jsps都具有相同的表单操作和相同的表单方法:
first.jsp看起来像这样:
<form:form method="POST" action="/ShowroomNav/requestQuote" id="requestQuoteForm">
<input type="hidden" value=${product.productCode } name="productCodes" />
<input type="hidden" id="requestQuoteEmailAddress" name="requestQuoteEmailAddress" />
</form:form>
Run Code Online (Sandbox Code Playgroud)
second.jsp看起来像这样:
<form:form method="POST" action="/ShowroomNav/requestQuote" id="requestQuoteForm">
<input type="hidden" id="requestQuoteEmailAddress" name="requestEmailAddress" />
<c:forEach var="product" items="${products}">
<input type="hidden" value=${product.productCode } name="productCodes" />
<div class="box">
<img
src="public/productImages/${product.productCode}/${product.productCode}A.jpg"
style="max-width: 100%"
onclick="productProfile('${product.productCode}')" /><br /> <label
class="name">${product.productName}</label>
</div>
</c:forEach>
</form:form>
Run Code Online (Sandbox Code Playgroud)
他们都通过javascript调用提交函数:
$("#requestQuoteSubmitButton").one("click",function(){
$("#requestQuoteEmailAddress").val($("#requestQuoteEmailAddressModal").val());
alert($("#requestQuoteEmailAddress").val());
$("#requestQuoteForm").submit();
});
Run Code Online (Sandbox Code Playgroud)
这就是controller.java的样子:
@RequestMapping(value = "/requestQuote", method = RequestMethod.POST) // or GET
public String requestQuote(@RequestParam("requestQuoteEmailAddress") String requestQuoteEmailAddress, @RequestParam("productCodes") String[] productCodes) {
System.out.println(">>>> requesting …Run Code Online (Sandbox Code Playgroud) 我有以下表格:
<form name="frmSearch" id="frmSearch" action="search_results.html" method="get">
<table>
<tr>
<td><input type="text" id="txtQuery" placeholder="What are you hungry for ?" required="true"></td>
</tr>
<tr>
<td><input type="text" id="txtLocation" placeholder="Your location" required="true">
</tr>
<tr>
<td align="middle"><input type="submit" placeholder="Let's Eat!" ></td>
</tr>
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
当我点击"Let's Eat"按钮(提交按钮)时,启动的新页面的URL不包含两个输入字段中的值:
http://localhost:8000/search_results.html?
Run Code Online (Sandbox Code Playgroud)
为什么是这样 ?
html ×9
forms ×4
javascript ×4
html5 ×3
input ×2
php ×2
asp.net ×1
asp.net-mvc ×1
attributes ×1
curl ×1
domdocument ×1
get ×1
java ×1
jquery ×1
jsp ×1
post ×1
spring ×1
spring-mvc ×1
web ×1