使用Apple.com上的javascript更改下拉选项,在我身上解决此午餐

问题描述 投票:1回答:1

[尝试使用javascript更改所选选项,但由于某些原因无法正常工作。遍历整个互联网寻找解决方案,但找不到解决方案。需要帮助 !

要获取源代码,请转到https://www.apple.com/shop/trade-in,位于“选择您的设备以获得折价估算”选择“智能手机”,“ Apple”,“ iPhone XS MAX”,选择“是”以保持良好状态,选择框“以礼品卡换购”。

然后您应该看到一个表单字段,该选项的默认状态选项不会为我动态更改。

现在尝试将控制台中的状态更改为MA(Massachusetts)。当前是默认的“状态”。

解决此午餐的第一个人在我身上,请下拉您的PayPal电子邮件。 :)

源代码:

<select aria-required="true" aria-describedby="tradeUp.shipping.moduleData.address-state-error" 
aria-invalid="false" data-autom="address-field-state" value="" 
data-recon-click="binding,actionClick,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-mouseover="binding,actionMouseOver,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-mouseout="binding,actionMouseOut,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-input="binding,setValue,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-change="binding,setValue,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-focusin="binding,setFocus,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-focusout="binding,clearFocus,tradeUp.shipping.moduleData.address.state,binding" 
id="tradeUp.shipping.moduleData.address-state" required="" 
class="form-dropdown form-dropdown-selectnone">

<option value="" selected="true">State</option>
<option value="AA">AA</option>
<option value="AE">AE</option>
<option value="AP">AP</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="AS">AS</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="GU">GU</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="MP">MP</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="PI">PI</option>
<option value="PR">PR</option>
<option value="PW">PW</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="US">US</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="VI">VI</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>

</select>


javascript html options
1个回答
0
投票

如果您用其不寻常的ID来处理select元素时遇到问题:您需要屏蔽其中的.字符,因为否则它们将被解释为表示类标识符的特殊字符。因此,document.querySelector()的选择器必须类似于'#tradeUp\\.shipping\\.moduleData\\.address-state'

在下面的其他几行中,您将找到一种更改select元素的当前选择的方法。在这种情况下,我将其更改为“ MA”。

document.querySelector('#change').addEventListener('click',function(){
  document.querySelector('#tradeUp\\.shipping\\.moduleData\\.address-state').innerHTML=
 ['abc','def','ghi','jki','lmn'].map(function(s){return '<option>'+s+'</option>';}).join('');
})

// in case you wanted to change the value of the
// select programmatically, do the following:

document.querySelector('#tradeUp\\.shipping\\.moduleData\\.address-state').value='MA';
<select aria-required="true" aria-describedby="tradeUp.shipping.moduleData.address-state-error" 
aria-invalid="false" data-autom="address-field-state" value="" 
data-recon-click="binding,actionClick,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-mouseover="binding,actionMouseOver,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-mouseout="binding,actionMouseOut,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-input="binding,setValue,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-change="binding,setValue,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-focusin="binding,setFocus,tradeUp.shipping.moduleData.address.state,binding" 
data-recon-focusout="binding,clearFocus,tradeUp.shipping.moduleData.address.state,binding" 
id="tradeUp.shipping.moduleData.address-state" required="" 
class="form-dropdown form-dropdown-selectnone">

<option value="" selected="true">State</option><option value="AA">AA</option>
<option value="AE">AE</option>
<option value="AP">AP</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="AS">AS</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="GU">GU</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="MP">MP</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="PI">PI</option>
<option value="PR">PR</option>
<option value="PW">PW</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="US">US</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="VI">VI</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>

</select>
<button id="change">change options</button>
© www.soinside.com 2019 - 2024. All rights reserved.