有人可以幫助我嗎? 如何更改沒有ID的輸入字段的值? 我無法更改輸入字段,因爲我正在與平臺進行集成,並且他們只能支持js。好的,第一個代碼用於讀取URL參數。 基本上,它會自動填充值到表單..更改輸入字段的值無標識
感謝
<html>
<head>
<script>
var get_params = function(search_string) {
var parse = function(params, pairs) {
var pair = pairs[0];
var parts = pair.split('=');
var key = decodeURIComponent(parts[0]);
var value = decodeURIComponent(parts.slice(1).join('='));
// Handle multiple parameters of the same name
if (typeof params[key] === "undefined") {
params[key] = value;
} else {
params[key] = [].concat(params[key], value);
}
return pairs.length == 1 ? params : parse(params, pairs.slice(1))
}
// Get rid of leading ?
return search_string.length == 0 ? {} : parse({}, search_string.substr(1).split('&'));
}
var params = get_params(location.search);
</script>
</head>
<body>
<!-- nanacast form -->
<tbody>
<tr id="customFieldsTR01">
<td id="customFieldsTR01td1">
<span style="font-weight: bold;" id="customFieldsTR01td1SPAN1">First Name </span>
</td>
<td id="customFieldsTR02td1">
<input type="text" name="u_firstname" value="">
<span id="*" class="tableRequired" style="font-weight: bold; color: red;">*</span>
</td>
</tr>
<tr id="customFieldsTR11">
<td id="customFieldsTR11td1">
<span style="font-weight: bold;" id="customFieldsTR11td1SPAN1">Last Name </span>
</td> \t \t \t \t \t \t <td id="customFieldsTR12td1">
<input type="text" name="u_lastname" value="">
</td>
</tr>
<tr id="customFieldsTR21">
<td id="customFieldsTR21td1">
<span style="font-weight: bold;" id="customFieldsTR21td1SPAN1">Email </span>
</td>
<td id="customFieldsTR22td1">
<input type="text" name="u_email" value="">
<span id="*" class="tableRequired" style="font-weight: bold; color: red;">*</span>
</td>
</tr>
</body>
<!-- not working since input fields do not have ids
<script>
document.getElementById("first_name").value = params['FirstName'];
document.getElementById("last_name").value = params['LastName'];
document.getElementById("email").value = params['EMail'];
</script>
-------->
</html>
使用'querySelector()'來定位必填字段 – Ramanlfc