簡體   English   中英

從文本字段獲取價值並在選擇字段中顯示

[英]Get value from text field and show in select field

嗨,我有cna有人幫我解決這個問題,我想從文本字段中獲取vlaue並使用javascript或jquery在select中顯示。

        <ul class="title_customer">
            <li>
        <label for="offaddress" class="title_register"><!--<em>*</em>--><?php echo $this->__('Title') ?></label>
        <div class="input-box">
           <div type="text" name="offaddress1" id="offaddress1" value="<?php echo $this->htmlEscape($this->getCustomer()->getOffaddress()) ?>" title="<?php echo $this->__('Office Address') ?>" class="input-text " >
            <input type="text" name="offaddress1" id="offaddress1" value="<?php echo $this->htmlEscape($this->getCustomer()->getOffaddress()) ?>" title="<?php echo $this->__('Office Address') ?>" class="input-text" />
           </div>
        </div>

    </li>
        <li>
        <label for="offaddress" class="title_register"><!--<em>*</em>--><?php echo $this->__('Choose Title') ?></label>
        <div class="input-box">
           <select type="text" name="offaddress" id="offaddress" value="<?php echo $this->htmlEscape($this->getCustomer()->getOffaddress()) ?>" title="<?php echo $this->__('Office Address') ?>" class="input-text " >
               <option value="DEP">DEP</option>
               <option value="DR">DR</option>
               <option value="MISS">MISS</option>
               <option value="MR">MR</option>
               <option value="MRS">MRS</option>
               <option value="MS">MS</option>
               <option value="SIR">SIR</option>
               <option value="LS">LS</option>
           </select>
        </div>
    </li>
    </ul>

如果用戶自行輸入,則可以在模糊功能內對選項運行循環。

var b;
$('#offaddress1').on('blur',function(){
  var a = $(this).val();        
  $('#offaddress option').each(function(){
    b = $(this).val();
    $(this).val(b + ' ' + a).html(b + ' ' + a);
  });
});

或者您可以通過執行類似的操作來讀取當前值(如果當前值已在頁面加載時填充)

var b;
var a = $('#offaddress1').val();        
$('#offaddress option').each(function(){
  b = $(this).val();
  $(this).val(b + ' ' + a).html(b + ' ' + a);
});

您可以/應該整理變量名並對其進行調整,以使其在頁面加載和模糊時運行。

問題是您在輸入和包裝div時使用了相同的ID,因此除非您進行更改,否則它將無法正常工作。

由於您未添加任何嘗試,因此我沒有對它們進行過長時間的測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM