2016-02-28 61 views
1

有人可以幫助我嗎? 如何更改沒有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&nbsp;</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&nbsp;</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&nbsp;</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>

+0

使用'querySelector()'來定位必填字段 – Ramanlfc

回答

1

您可以使用document.querySelector()並通過名稱找到他們:

document.querySelector('input[name="u_firstname"]').value = 'New value' 
+0

謝謝!有效 :) – RustyBrain

-1

由於所有的你的代碼中的元素有名字嘗試:

document.getElementsByTagName("u_firstname"); 
相關問題